Learn how to reload or refresh page in javascript. There are hundreds of different methods which you can use to refresh or reload the page.
We will see few methods which you can use with HTML and/or javascript.
Using history object
We can refresh the current page by using browser history .go()
method.
<input type="button" value = "Refresh" onclick="history.go(0)" />
Setting the meta tag
We can also set the meta tag http-equiv which will automatically refresh the page at a given interval.
<meta http-equiv="refresh" content="1">
This will reload the page after the specified seconds of time. Here content="1"
is equal to 1 second.
Using window.location.reload()
We can also use window.location.reload()
method to reload or refresh the current page.
window.location.reload(); //OR location.reload();
location.reload(true/false)
method takes a parameter to determine how the page should be reloaded.
- True:- Will reload the page from server(uncached).
ing
- False (Default):- Will reload the cached page.
Passing the true/false parameter is deprecated, you can just call the method without any parameters for reloading, otherwise use other methods.
Refreshing page with window.location.href
window.location.href
method returns/loads the current url. So we can use it to reload/refresh the page in javascript.
window.location.href = window.location.href;
It will reopen the page with current url.
Nemanja says:
location.reload(true/false) is deprecated.
What is the alternative to force reload?
Prashant Yadav says:
Use the other methods
Roger Virtuoso says:
You can use window.location.reload() without passing any parameters.
Anubhav says:
window.location.reload keeps on reloading page..I want just one reload ..how can we do that ?
Prashant Yadav says:
It depends upon on how you call it. Call it under certain condition to stop getting into infinite loop.