views:

288

answers:

1

Hey Guys, 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?

+2  A: 

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
The second method (.reload) is not work in some cases.
Aristos
so, "document.location.reload(true)" is cross-browser secure and the most robust one?
Mel
@Mel, I believe so. Really, window and document should both work.
tloflin
note that when you use document.location.reload() on a POST the browser will ask you if you want resend the data to reload the page.
Wimmel