www.webdeveloper.com
Results 1 to 4 of 4

Thread: Auto-refresh page once only after first load

  1. #1
    Join Date
    Jan 2003
    Location
    UK
    Posts
    267

    Auto-refresh page once only after first load

    Hi all,

    I have a web page which isn't working as planned in IE9 (no surprise here). A quick fix is to have the page auto-refresh itself once only after first loading (not continuously). I've tried a few things, like [icode]<body onload="window.location.reload()">[/icode] but this makes it spaz out and refresh repeatedly. Which is odd.

    Please let me know if you have any ideas.

    thank you

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by W8 4me View Post
    I've tried a few things, like
    Code:
    <body onload="window.location.reload()">
    but this makes it spaz out and refresh repeatedly. Which is odd.
    What's odd about that? It's doing exactly what you asked. The real solution is to find the cause, however this will give you a single reload:
    Code:
    <script type='text/javascript'>
    
    (function()
    {
      if( window.localStorage )
      {
        if( !localStorage.getItem( 'firstLoad' ) )
        {
          localStorage[ 'firstLoad' ] = true;
          window.location.reload();
        }  
        else
          localStorage.removeItem( 'firstLoad' );
      }
    })();
    
    </script>
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    Note that Ali's script may not work as intended if the user has disabled cookies/storage.

  4. #4
    Join Date
    Feb 2013
    Posts
    1

    Red face Code:

    <script type="text/javascript">
    $(document).ready(function(){

    //Check if the current URL contains '#'
    if(document.URL.indexOf("#")==-1)
    {
    // Set the URL to whatever it was plus "#".
    url = document.URL+"#";
    location = "#";

    //Reload the page
    location.reload(true);

    }
    });
    </script>
    Due to the if condition the page will reload only once.

Thread Information

Users Browsing this Thread

There are currently 4 users browsing this thread. (0 members and 4 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles