Javascript Location Reload
Reload/refresh a page with javascript
To reload or refresh the current page with javascript, you can use the reload() function.
To make a link that refreshes the current page you can use the code below:
<a href="#" onclick="location.reload(true)">refresh</a>
This link will appear like this:
I recommend that you include the current page URL within the href="" section, to ensure that users with javascript disabled or unavailable have a chance too!
Alternative technique
I found at some point in the past that this didn't always work, so instead you can ask the browser to load the current page. This method is not recommended, however, as it should show you a local cache of the page.
<a href="#" onclick="history.go(0)">refresh</a>
This link will appear as below:
21.02.2007. 19:09
DavidP said on 22.02.2007. 21:07
As you said in your post, the 'alternative method' doesn't reload the page at all unless the visitor doesn't have a cache, so I don't recommend anyone actually use it.
Andy said on 01.03.2007. 19:11
Very true - this article is actually a few years old, and for some reason the second method worked for me then. But that was probably me not understanding caching at the time!
I use history.go(-1) to simulate the back button.
Does location.reload(false) do anything special?
Andy said on 31.07.2007. 20:08
>>history.go(-1) to simulate the back button.
Yeh, this is typically what the history object is for, although I have seen some inconsistencies cross-browser.
>>location.reload(false)
This is the same as location.reload(), I believe, which I think is supposed to reload from the browser cache.
Location.reload(true) should force a reload from the server.
greg said on 05.11.2008. 22:41
I am using in the code behind:
string script = " location.reload(true); ";
this.Clientscript.RegisterStartupscript(script.GetType(), "referesh", script);
And I've got a strenge message from internet explorer which ask me to click Retry or Cancel and it loop if I press Retry (Cancel do nothing, but the PopUp Message stay there).
Any ideea?
Recent Responses:
Page last (manually) updated: October 20, 2008.
Questions, comments, insults or praise? Have your say: