How can I reload the page using JavaScript?
I need a method that works in all browsers.
window.location.reload();
// If we needed to force the document to be fetched from the
// web server again (such as where the document contents
// change dynamically but cache control headers are not
// configured properly), Firefox supports a non-standard
// parameter that can be set to true to bypass the cache:
//window.location.reload(true);
window.location.replace(window.location.pathname + window.location.search + window.location.hash);
// does not create a history entry
window.location.href = window.location.pathname + window.location.search + window.location.hash;
// creates a history entry
location.reload(forceReload: boolean)
is deprecated now (just the true
/false
parameter), though it might still work.
nocache=' + (new Date()).getTime()
.
location.reload();
See this MDN page for more information.
If you are refreshing after an onclick
then you'll need to return false directly after
location.reload();
return false;
location.reload()
and window.location.reload()
?
location
is the same as window.location
as window
is the global object.
Dec 20, 2013 at 9:48
window.location.reload();
for readability, as location
could be a local variable - whereas you'd usually avoid variables of the name window
.
return false;
is not strictly necessary in a handler: doesn't reload
supersede any further processing?
Aug 29, 2020 at 1:05
I was looking for some information regarding reloads on pages retrieved with POST requests, such as after submitting a method="post"
form.
To reload the page keeping the POST data, use:
window.location.reload();
To reload the page discarding the POST data (perform a GET request), use:
window.location.href = window.location.href;
Hopefully this can help others looking for the same information.
POST
request.
Jan 24, 2018 at 23:09
window.location.href = window.location.href
does not reload the page if the current url contains a #
. You could remove the hash if you don't need it window.location.href = window.location.href.split('#')[0];
.
Mar 10, 2020 at 14:41
You can perform this task using window.location.reload();
. As there are many ways to do this but I think it is the appropriate way to reload the same document with JavaScript. Here is the explanation
JavaScript window.location
object can be used
window
: in JavaScript represents an open window in a browser.
location
: in JavaScript holds information about current URL.
The location
object is like a fragment of the window
object and is called up through the window.location
property.
location
object has three methods:
assign()
: used to load a new documentreload()
: used to reload current documentreplace()
: used to replace current document with a new oneSo here we need to use reload()
, because it can help us in reloading the same document.
So use it like window.location.reload();
.
To ask your browser to retrieve the page directly from the server not from the cache, you can pass a true
parameter to location.reload()
. This method is compatible with all major browsers, including IE, Chrome, Firefox, Safari, Opera.
replace()
turned out to be the solution I was looking for because I needed to reload my page with a slight change in the query string.
Jul 1, 2015 at 0:01
Try:
window.location.reload(true);
The parameter set to 'true' reloads a fresh copy from the server. Leaving it out will serve the page from cache.
More information can be found at MSDN and in the Mozilla documentation.
This works for me:
function refresh() {
setTimeout(function () {
location.reload()
}, 100);
}
If you put
window.location.reload(true);
at the beginning of your page with no other condition qualifying why that code runs, the page will load and then continue to reload itself until you close your browser.
location.href = location.href;
This should work:
window.location.href = window.location.href.split( '#' )[0];
or
var x = window.location.href;
x = x.split( '#' );
window.location.href = x[0];
I prefer this for the following reasons:
Alternatively, you may use the most recent official method for this task
window.location.reload()
The Javascript reload() method is used to reload the current document or URL. The javascript location.reload(true) method work just like reload button in your browser. By default, the JS reload() method reloads the page from the cache, however you may force it to reload the page from the server side by setting the forceGet parameter to true: location. reload(true). Source: https://www.coderepublics.com/JavaScript/javascript-location-reload-true.php
What about Depricated? It is only the reload with forcedReload which is now deprecated. But to avoid depricated error you can use location.reload() without the forceReload flag.