Preferred method to reload page with JavaScript?

JavascriptReload

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMelView Question on Stackoverflow
Solution 1 - JavascripttloflinView Answer on Stackoverflow
Solution 2 - Javascriptvol7ronView Answer on Stackoverflow