Reload page javascript after seconds

  • « Back

How to auto refresh page using JavaScript, Here are JavaScript confirm box, when its confirm after page refresh automatically.

Example

<html>
<head>
    <title>JavaScript refresh page</title>
</head>
<body>
    <a href="javascript:location.reload(true);">Refresh this page</a>
</body>
</html>

Example Result

After 5 seconds page refresh

Example

<html>
<head>
    <title>JavaScript page refresh example</title>
    <script type="text/JavaScript">
        function timeRefresh(timeoutPeriod) {
            setTimeout("location.reload(true);", timeoutPeriod);
        }
    </script>
</head>
<body onLoad="JavaScript:timeRefresh(5000);">
  <h3>This page will auto refresh after 5 seconds.</h3>
</body>
</html>

Run it...   »

Example Result

User confirm after 5 seconds page refresh

Example

<html>
<head>
    <title>JavaScript page refresh example</title>
    <script language="javascript" type="text/javascript">
        function confirm_refresh() {
            var Refresh = confirm("Do you want to refresh the page?");
            if (Refresh) {
                setTimeout("location.reload(true);", 5000);
            }
        }
    </script>
</head>
<body>
    <p>Click Here: <a href="javascript:confirm_refresh();">Refresh this page after 5 seconds.</a></p>
</body>
</html>

Run it...   »

Example Result

  • « Back


You can refresh a web page using JavaScript location.reload method. This code can be called automatically upon an event or simply when the user clicks on a link. If you want to refresh a web page using a mouse click, then you can use the following code −

<a href="javascript:location.reload(true)">Refresh Page</a>

To understand it in better way you can Refresh Page.

Auto Refresh

You can also use JavaScript to refresh the page automatically after a given time period. Here setTimeout() is a built-in JavaScript function which can be used to execute another function after a given time interval.

Example

Try the following example. It shows how to refresh a page after every 5 seconds. You can change this time as per your requirement.

<html>
   <head>
      
      <script type = "text/JavaScript">
         <!--
            function AutoRefresh( t ) {
               setTimeout("location.reload(true);", t);
            }
         //-->
      </script>
      
   </head>
   
   <body onload = "JavaScript:AutoRefresh(5000);">
      <p>This page will refresh every 5 seconds.</p>
   </body>
   
</html>

Output

This page will refresh every 5 seconds.

javascript_page_redirect.htm

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:

  • Refreshing a Page in JavaScript
  • Auto Refresh Page After 5 Second
  • Refresh Page on Button Click
  • Using History Function
  • Refresh Page in HTML
  • Using Jquery

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.

How do you refresh a page every 5 seconds?

setTimeout(function () { location. reload(1); }, 5000);

How do I refresh a page after some time?

Auto Refresh You can also use JavaScript to refresh the page automatically after a given time period. Here setTimeout() is a built-in JavaScript function which can be used to execute another function after a given time interval.

How do you refresh a page using JavaScript?

You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.

Are used to specify a delay in second before the browser can automatically reload the document?

Meta refresh is a method of instructing a web browser to automatically refresh the current web page or frame after a given time interval, using an HTML meta element with the http-equiv parameter set to " refresh " and a content parameter giving the time interval in seconds.