Different Ways To Reload/Refresh Page In Javascript

Different Ways To Reload/Refresh Page In Javascript

Find out how to initiate a reload of a page using a javascript method. You can refresh or reload the page using a variety of techniques.

Using HTML and/or javascript, we’ll demonstrate a few techniques.

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

Meta tag http-equiv can be used to automatically refresh the page at a predetermined period.

<meta http-equiv="refresh" content="1">

After the selected number of seconds, the page will be reloaded. Here content=”1″ is equal to 1 second.

Using window.location.reload()

We can use window.location.reload() method to reload or refresh the current page.

window.location.reload();

//OR

location.reload();

location.reload(true/false) function takes an argument to decide how the page should be reloaded.

True:- Will reload the page from server(uncached) (uncached). ing

False (Default):- Will refresh the cached page.

Passing the true/false parameter is deprecated, you can merely call the function without any parameters for reloading, otherwise use other ways.

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 the current URL.