用 JavaScript 重新加载页面的首选方法?

您更喜欢用哪种方式(使用按钮)重新加载当前页面?

1 <input type="button" value="Reload" onClick="history.go(0)">
2 <input type="button" value="Reload" onClick="location.reload(true)">
3 <input type="button" value="Reload" onClick="window.location.reload(true)">
4 <input type="button" value="Reload" onClick="window.location.href=window.location.href">
5 <input type="button" value="Reload" onClick="document.location.reload(true)">
6 <input type="button" value="Reload" onClick="document.location.href=document.location.href">

由于网页的 URL 经常变化 AFAIK 一个“回退功能”,如

<a href="urlOfCurrentPage.html" onclick="window.location.reload(true);return false;">Reload</a>

对我没用,对吧?

68949 次浏览

Depends on what you want to do. The fourth and sixth methods there won't reload any form data, they essentially make a separate visit to the page. Some versions of Firefox also have issues with the third method. Other than that, I'd go with the fifth as a personal preference. It seems the clearest.

You may also do:

wd represents window || document:

  • wd.location.assign(wd.location.href) : go to the URL
  • wd.location.replace(wd.location.href) : go to the URL and replace previous page in history
  • wd.location.reload(<true/false/blank>) : reload page from server/cache/cache