There are several ways to refresh a page in JavaScript. The most common method is to use the ‘location.reload()’ function, which reloads the current page with the current URL.

Here’s an example:

location.reload();

This function can also be passed a Boolean parameter, which, when set to ‘true’, reloads the page from the server (bypassing the browser cache), while when set to ‘false’ reloads the page from the cache.

location.reload(true);

Another way to refresh a page is to use the ‘location.href’ property, which sets the current URL to a new value, causing the browser to load the new page.

location.href = location.href;

You can also use ‘location = location’ which does the same as above.

location = location;

Lastly, you can use ‘history.go(0)’ which reloads the current page by going back to the previous page and then forward again.

history.go(0);

It is important to note that these methods will cause the page to lose any data that is currently in the form fields, so it’s not recommended to use these methods if the user has entered data on the page that has not been saved.

Also Read:

Categorized in: