How to Refresh/Reload Page or Part of the Page Automatically

Here I am going to show different methods to refresh/reload the page or part of the page automatically in certain period of time.

Simplest way to refresh whole page is by using meta tag as below:

  <meta http-equiv="refresh" content="30;url=index.html">

Above code refreshes the page for every 30 seconds.

if you don’t want to use meta tag as some browsers won’t support, you can do this by using Javascript as below:

<script type="text/javascript">
    window.onload = setupRefresh;

    function setupRefresh() {
      setTimeout("refreshPage();", 30000); // milliseconds
    }
    function refreshPage() {
       window.location = location.href;
    }
  </script>

Above code reloads the page for every 30 seconds.
setTimeout() allows you to specify that a piece of JavaScript code (called an expression) will be run a specified number of milliseconds from when the setTimeout() method was called. The general syntax of the method is:

setTimeout ( expression, timeout );

where expression is the JavaScript code to run after timeout milliseconds have elapsed.

Refreshing part of a page periodically

Using Frames

Divide the page using frames and load different pages in each frame as below:

<html>
  <head>
   <title></title>
  </head>
  <frameset id="cntfrm" cols="250,*">
      <frame src="index1.html" scrolling="auto" frameborder="1" />
      <frame src="index2.html" scrolling="auto"  />
  </frameset>
</html>

In the above two parts, if you want to reload particular part of the page, then use the above meta tag or Javascript approach in frame source html page.
Or
You can use the frame “reload” function periodically in frameset page itself.

<html>
  <head>
   <script type="text/javascript">
      window.onload = setupRefresh;

      function setupRefresh() {
          setInterval("refreshFrame();", 1000);
      }
      function refreshFrame() {
         parent.right_frame.location.reload();
      }
  </script>
  </head>
  <frameset id="cntfrm" cols="250,*">
	<frame name="left_frame" src="test.html" scrolling="auto" frameborder="1" />
	<frame  name="right_frame" scrolling="auto" src="test2.html" />
  </frameset>
</html>

In the above code, “right_frame” reloads every second.
setInterval() function is very closely related to setTimeout() – both have similar syntax:

  setInterval ( expression, interval );

The important difference is that, whereas setTimeout() triggers expression only once, setInterval() keeps triggering expression again and again (unless you tell it to stop). You can stop setInterval() by calling method clearInterval().

Without Frames

We can reload the particular block of the page using Javascript. Here, I am going to explain this with display time on a page which reloads every second.

<html>
  <head>
   <title></title>
  <script type="text/javascript">
    window.onload = startInterval;
    function startInterval()
    {
        setInterval("startTime();",1000);
    }
    
    function startTime()
    {
        document.getElementById('time').innerHTML = Date();  
    }
  </script>
  </head>
  <body>  
            <div id="time"></div>
  </body>
</html>

Above code will display time with seconds on a page and refreshes div (id “time”) block every second to show the exact time.

If you are using a JQuery, then use the “load” function to load the page in div block.

 <script type="text/javascript">
    window.onload = setupRefresh;
    function setupRefresh()
    {
        setInterval("refreshBlock();",30000);
    }
    
    function refreshBlock()
    {
       $('#block1').load("index.html");
    }
  </script>

  <body>  
            <div id="block1"></div>
            <div id="block2"></div>
  </body>

In the above code, index.html page loads every 30 seconds in div with id “block1″.

You can leave a response, or trackback from your own site.

28 Responses to “How to Refresh/Reload Page or Part of the Page Automatically”

  1. [...] here: http://www.techtricky.com/how-to-refresh-page-or-part-of-the-page-automatically/ .post_btns {margin-top: 10px;} .btns_left {float:left;} .btns_right {float:right;} Share [...]

  2. Good article! Keep it up!

  3. sushant says:

    nice one

  4. Manikandan says:

    Useful post.
    Thanks

  5. hyunjungsoh says:

    Thank you. :)

  6. Fil says:

    Helped me out, thanks good job!

  7. tommy says:

    thanks, I use this for my project.

  8. Maspray says:

    Are those script works on Blogspot platform?
    I’ve placed some COUNTDOWN TIMER script on my blog, So, i want to make my Page reload if the COundown Timer finished. This CountDown script allow me to set into an exact Date and time, like 4/12/2012 at 9.35 PM. How about those Script?

    • admin says:

      call the refreshPage only when your counter finished, like this :

      function setupRefresh() {
      if(count_down_timer) {
      setTimeout(“refreshPage();”, 1000); // milliseconds
      }
      }

  9. Brandon says:

    How do you refresh only once after 1 second?

    • admin says:

      you just need to add some hash on first load and don’t refresh if it has hash, like this:

      window.onload = setupRefresh;

      function setupRefresh() {
      if(!window.location.hash) {
      setTimeout(“refreshPage();”, 1000); // milliseconds
      }

      }
      function refreshPage() {
      window.location += ‘#loaded’;
      window.reload();
      }

      If your url already has hashes then append and look for the added hash or use the query parameters.

      • Ghousia Naaz says:

        Hi ,

        Can you please tell me is it possible that I can set a BSP parameter dynamically whenever the page is refreshed i tried doing so but it sets the value the first time the page is loaded .
        window.onload = setupRefresh;
        function setupRefresh()
        {
        setTimeout(“refreshPage();”, 60000);
        }
        function refreshPage()
        {
        window.location = location.href;

        }
        The value 21 is being set the first the window is loaded . Is there any way i can set this value only after the first refresh ? Please let me know

  10. jatinder says:

    Thanks for this tutorial.
    Im looking for it from very long time.

    Via jquery it stops working if i need to refresh two different divs, with different urls on same page….

  11. tomislav says:

    Web site has been made: for example, 01/01/2012, 05/30/2012 redesigned.
    It is accessed 02/02/2012. How to make a page reload after 05/30/2012. . If you visit site the first time after 30/05/2012, must not be automatically re-load. How to do it.
    thank you

  12. bob s says:

    Will this work with a div ‘header’ where the header is php rotating image? I have php rotating image that works great easy to + or – images, but want to have it rotate. This looks like much simpler way if can refresh the div that is image rotation, rather than load every photo into an array and use js to make slide show.

    Will try it as soon as ftp uploader working – issues with Godaddy.

    Any one have any other ideas on how to do this.

  13. Bhavik says:

    if i want to use this same for advertisement, only advertisement will reload after several sec then what i have to do???

  14. rajesh paul says:

    ur suggestion was very much helpful. but when the back button is used the one time refresh technique u specified is not working for a page. give me a soln.

  15. Mahender Reddy says:

    Hi,
    How to refresh a page only once per a visitor? I do not want to refresh it on time babsis. I want it by visitor basis? Is it possoble?.. Pelase suggest..
    Apperciate if you can respond soon..
    Thanks in advance

    Reddy

  16. sahar says:

    its really nice code…thanks

  17. Travis says:

    Nice article! It actually was the perfect little snippet i needed for my app, Thank you

  18. Shina says:

    Nice article and thanks for sharing the codes. it helped me alot.

  19. Shiv says:

    Can we realod multiple pages like for Eg: google, yahoo, gmail. by using buttom?

  20. priya says:

    i have a a select,and i have to call window.reload on change of select.
    how to change title of that page and retain the value selected during refresh.

  21. Reyhaneh says:

    Thanx a lot! useful article.

  22. Thanks alot, very good article

  23. captain says:

    please help me i cant figure this out, i would like to use the time vision by it should refresh my div myposts to show new content not the time

  24. Doctor_E says:

    Excellent code, thanks! But I would like to ask regarding the “Without Frames” example, in case instead of Date() we would like to refresh the existing content …, which would be the code?

    In my case, I use a div which includes some javascripts that get some info from a server and print this info on the webpage, so I would like to update the script execution every few seconds.

    Thanks

Leave a Reply