Preferred method to reload page with JavaScript? [closed]

青春壹個敷衍的年華 提交于 2019-11-26 07:18:04

问题


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

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

won\'t work for me, right?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/2624111/preferred-method-to-reload-page-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!