Blog Archive

jQuery reload HTML page at regular intervals

To refresh or reload a page using jQuery, you can use the location.reload() method. Source code for a sample HTML page that reloads after you click a button on the page.


<html>
<head>
<title>jQuery Refresh page example</title>

<link
 href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"
 rel="stylesheet" type="text/css" />

<script
 src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"
 type="text/javascript"></script>
<script
 src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"
 type="text/javascript"></script>
<script type="text/javascript">
        $(document).ready(function() {
            $('#myButton').click(function() {
                location.reload();
            });
        });     
    </script>

</head>
<body>
 <input id="myButton" type="button"
  value="Click here to Refresh the Page" />
</body>
</html>

How to refresh a page at regular intervals in jQuery


To refresh a page at regular intervals you can use setInterval() method along with the location.reload(). Source code for a sample HTML page that will refresh every 5 seconds.


<html>
<head>
<title>jQuery Refresh page example</title>

<link
 href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"
 rel="stylesheet" type="text/css" />

<script
 src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"
 type="text/javascript"></script>
<script
 src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"
 type="text/javascript"></script>
<script type="text/javascript">
        $(document).ready(function() {
         setInterval("location.reload();", 5000);
         $("#someInfo").html("Time is " + new Date());
        });     
    </script>

</head>
<body>
 <p>This page will Refresh every 5 seconds!</p>
 <div id="someInfo"></div>

</body>
</html>