使用 JavaScript 重新加载页面的首选方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2624111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Preferred method to reload page with JavaScript?
提问by Mel
which way to reload a current page (using a button) would you prefer?
您更喜欢哪种方式来重新加载当前页面(使用按钮)?
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">
As the URL of the page changes frequently AFAIK a 'fallback function' like
由于页面的 URL 经常更改 AFAIK 一个“后备功能”,如
<a href="urlOfCurrentPage.html" onclick="window.location.reload(true);return false;">Reload</a>
won't work for me, right?
不会为我工作,对吧?
采纳答案by tloflin
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.
取决于你想做什么。第四和第六种方法不会重新加载任何表单数据,它们本质上是对页面进行单独访问。某些版本的 Firefox 也存在第三种方法的问题。除此之外,我会根据个人喜好选择第五个。好像最清楚了。
回答by vol7ron
You may also do:
你也可以这样做:
wdrepresents window|| document:
wd代表窗口|| 文件:
- 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
- wd.location.assign(wd.location.href): 转到 URL
- wd.location.replace(wd.location.href): 转到 URL 并替换历史记录中的上一页
- wd.location.reload(<true/false/blank>):从服务器/缓存/缓存重新加载页面

