Here’s a short guide to refreshing a webpage with JavaScript, with examples.
There are over 500 ways to trigger a page reload using JavaScript. All but one are unofficial or are the side effect of another behavior.
Here’s how to do it properly.
Using location.reload()
Simply call:
location.reload()
…anywhere in your code to trigger a page reload. It’s that easy.
What about window.location.reload() ?
You may see some use:
window.location.reload()
This is exactly the same as using location.reload() – the window object in JavaScript is the global context, so there’s usually no need to specify it explicitly when running location.reload().
If for some reason, you have another variable called location and there is a conflict, you can specify window.location.reload() to get around this.
See more about scopes in JavaScript here.
Reloading from onclick Attribute or Event
You can call for a reload from an HTML elements onclick attribute directly:
<a href="#" onclick="location.reload()">Reload</a>
More Information
The location.reload() function doesn’t accept any parameters (it may have in the past in some browsers but is no longer officially supported).
Find out more about it here:
https://developer.mozilla.org/en-US/docs/Web/API/Location/reload