Different ways to reload/refresh page in javascript

Learn how to reload or refresh page in javascript. There are hundreds of different methods which you can use to refresh or reload the page.

We will see few methods which you can use with HTML and/or javascript.

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

We can also set the meta tag http-equiv which will automatically refresh the page at a given interval.

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

This will reload the page after the specified seconds of time. Here content="1" is equal to 1 second.


Using window.location.reload()

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

window.location.reload();

//OR

location.reload();

location.reload(true/false) method takes a parameter to determine how the page should be reloaded.

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

    ing

  • False (Default):- Will reload the cached page.

Passing the true/false parameter is deprecated, you can just call the method without any parameters for reloading, otherwise use other methods.


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 current url.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *