How to Reload a Page using JavaScript
To reload a page using JavaScript, you can use the location
object or the history
object. Here are a few methods you can use:
Using
location.reload()
Method:This method reloads the current document. You can optionally pass
true
as a parameter to force a reload from the server rather than from the cache.// Reloads the page from the cache location.reload(); // Reloads the page from the server (ignores cache) location.reload(true);
Using
location.href
:You can set
location.href
to the current URL to reload the page.// Reloads the page location.href = location.href;
Using
location.replace
:This method replaces the current page with a new one. It doesn’t keep the current page in the session history, so it will not be possible to use the “back” button to return to it.
// Reloads the page location.replace(location.href);
Using
history.go(0)
:This method is another way to reload the page.
history.go(0)
is equivalent tolocation.reload()
.// Reloads the page history.go(0);
All of these methods effectively reload the page, but location.reload()
is generally the most straightforward and commonly used approach.