Register Login

How to Refresh a Web Page in JavaScript?

Updated Mar 02, 2021

While using JavaScript, you may want to refresh a web page with your code. Let us look at the different ways to do so.

We will here learn the following methods to refresh a web page in JavaScript:

1. Refreshing a Page in JavaScript

In JavaScript, page is reloaded using the document.location.reload() method or the window.location.reload() method. The location.reload() method gives the same result as pressing the reload button on your browser.

This method reloads the page from directly the browser’s cache by default. If the forceGet property is set to true, the web page will be reloaded from the server.  

Reload Page from Cache

Example

<html>
<head>
<title>Page Reload Uisng Javascript</title>
<script>
location.reload();
</script>
</head>
<body>
</body>
</html>

Reload Page from Server (without Cache)

<html>
<head>
<title>Page Reload Uisng Javascript without Cache</title>
<script>
location.reload(true);
</script>
</head>
<body>
</body>
</html>

The default parameter is False here. So, if the parameter is left blank, object.reload() reloads the page using the broswer’s cached data, i.e. is identical to using the method as object.reload(false).

2. Auto Refresh Page After 5 Second

<html>
<head>
<title>Page Reload Uisng Javascript after 5 seconds</title>
<script>
setTimeout("location.reload(true);", 5000);
</script>
</head>
<body>
</body>
</html>

You can also use JavaScript to refresh the web page automatically after a specified interval. Using the setTimeout() method here, we are refreshing the page automatically 5 seconds after it reloads.

3. Refresh Page on Button Click

<html>
<head>
<title>Page Reload Uisng Javascript on button click </title>
</head>
<body>
<input type="button" value="Reload Page" onClick="window.location.reload(true)">
</body>
</html>

Instead of automatically refreshing a web page using a method, you can call the method when a user performs an event, such as a button click. In this example, you can see that page will get refreshed using the location.reload() method after the user clicks on the Reload Page button.

4. Using History Function

Example

<html>
<head>
<title>Page Reload Uisng Javascriptusing history function</title>
</head>
<body>
<input type="button" value="Reload Page" onClick="history.go(0)">
</body>
</html>

The JavaScript, the window object has a history property which is used for page refreshing. The history.go() method in this example helps in manipulating the browser session history.

Here 0 is the history parameter,

  • 0 = current Page
  • -1 = Previous page

This parameter lets you navigate back and forth within your web page session history.

5. Refresh Page in HTML

<html>
<head>
<title>Page Reload Uisng meta tag</title>
	<meta http-equiv="refresh" content="1">
</head>
<body>
</body>
</html>

The code will refresh the HTML document after every 1 second. The http-equiv is set to refresh and the content attribute is set to the interval of 1 second. 

6. Using Jquery

<html>
<head>
<title>Page Reload Uisng Jquery</title>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$( document ).ready(function() {
    $("body").load(window.location.href);
});
</script>
</head>
<body>
</body>
</html>

The window.location has a href property. This returns the URL of the current website. In this example, this property is used for reloading and refreshing the current page. This property can also be used to target another page and refresh it.


×