105

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?

idmean
  • 14,540
  • 9
  • 54
  • 83
Mel
  • 1,061
  • 2
  • 8
  • 5
  • Note that numbers 2 and 3 are the same thing – Matti Virkkunen Apr 12 '10 at 18:00
  • 3
    Number 4 and 6 will not reload anything if there is a # present in the url – jontro Aug 07 '12 at 12:31
  • 2
    Number 2, 3 & 5 aren't implemented by all browsers, use `*.location.reload()` as the argument is only used to specify if the cache should be ignored. http://stackoverflow.com/questions/10876244/browser-support-for-window-location-reloadtrue – Dead.Rabit Oct 25 '13 at 11:39
  • OP missed the most preferred method of all: location.reload(); – Doug S Sep 30 '15 at 06:45

2 Answers2

56

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.

tloflin
  • 4,050
  • 1
  • 25
  • 33
30

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
vol7ron
  • 40,809
  • 21
  • 119
  • 172