Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Location
    Lahore
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to Stop Page Reload ?

    Hi,

    I am using javascript function for refreshing html page. I have used [window.location.reload(true);] method to reload page from server. This code works if i use it by using Link/button etc.
    For Example: <input type="button" value="Refresh" onclick="window.location.reload(true);" />

    But i want to refresh page when page loads.
    For Example: <body onload = "window.location.reload(true);>
    But this time, page reloads in an infinite loop. i don't know why...

    I have also used following function (against body onLoad) to reload and then stop, but this also does'nt work.

    function doLoad(){
    var timeoutId = setTimeout( window.location.reload(true), 1000 );
    window.clearTimeout(timeoutID);
    }

    Is there a way to stop reloading page after one refresh ?

    Thanks...

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    I do not understand why refresh a page immediately after it loads... If you are trying to avoid caching of your web page why not use meta tags instead?
    Code:
    <meta http-equiv="pragma" content="no-cache" />
    I am still learning English

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by farhanBajwa View Post
    But i want to refresh page when page loads.
    For Example: <body onload = "window.location.reload(true);>
    But this time, page reloads in an infinite loop. i don't know why...


    Is there a way to stop reloading page after one refresh ?
    Why on earth do you want to refresh the page as soon as it loads? Naturally you are creating an infinite loop - every time the page loads you tell it to reload.

    You cannot prevent the user from refreshing the page.


    "Think about how stupid the average person is, and then realize that half of them are more stupid than that!" - George Carlin

  • #4
    New to the CF scene
    Join Date
    Jul 2009
    Location
    Lahore
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The purpose of Reload is to get page from the server rather than from cache. I have used all Meta tags but page still cached.

    Is there any other way to avoid from cache except Meta tags (as they are not working) ?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Try this:-


    Code:
    <?php header("Expires: 0"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("cache-control: no-store, no-cache, must-revalidate"); header("Pragma: no-cache");?> 
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
    <head> 
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> 
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
    <meta http-equiv="expires" content="FRI, 13 APR 1999 01:00:00 GMT">

  • #6
    New to the CF scene
    Join Date
    Jul 2009
    Location
    Lahore
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am not using PHP... but i have tried all the Meta tags related to Cache but no gain.

    Any other idea ?

  • #7
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Here is possible JavaScript solution:
    Code:
    <script type="text/javascript">
       function refreshMe() {
          var _now = new Date();
          var _prev = 0;
          var indx = location.search.indexOf("time=");
          if (indx != -1) _prev = parseInt(location.search.slice(indx + 5));
          if (_prev < _now.getTime() - 5000) {
             indx = location.href.indexOf("?");
             if (indx == -1) indx = location.href.length;
             location.replace(location.href.slice(0, indx) + "?time=" + _now.getTime());
          }
       }
       window.onload=refreshMe;
    </script>
    This will not result in infinite loop since the script comares the current time with the time of previous refresh. If the difference is greater than 5 seconds then it refreshes the page. =)

    P.S. The script is meant for the case when location string has no GET parameters on default...
    I am still learning English

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by farhanBajwa View Post
    I am not using PHP... but i have tried all the Meta tags related to Cache but no gain.

    Any other idea ?
    Nope, AFAIK if that does not work then nothing more can be done.

    I believe that the PHP script does work, but in most browsers the META tags are ignored.

  • #9
    New to the CF scene
    Join Date
    Jul 2009
    Location
    Lahore
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have used Javascript solution Amphiluke has provided, but i redirects my page back to the Index page. For Example, I have clicked a button from Page1 to go to next page(Page2). From Page2, i have called RefreshMe() against body onLoad(), then it redirects my page back to Page1. As i want to refresh Page2 but Page2 doesn't display at all.

    How can i access my current page (Page2) after refreshing ?

    Thanks...

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Place in your head section (between <head> and </head> tags):

    meta HTTP-EQUIV="Pragma"; CONTENT="no-cache">
    meta HTTP-EQUIV="Expires"; CONTENT="-1";>

    And for IE then add the same code just before the ending </html> tag:

    <head>
    meta HTTP-EQUIV="Pragma"; CONTENT="no-cache">
    meta HTTP-EQUIV="Expires"; CONTENT="-1";>
    </head>
    </html>

    If that does not work, then nothing (client-side) will.

  • #11
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Quote Originally Posted by farhanBajwa View Post
    I have used Javascript solution Amphiluke has provided, but i redirects my page back to the Index page. For Example, I have clicked a button from Page1 to go to next page(Page2). From Page2, i have called RefreshMe() against body onLoad(), then it redirects my page back to Page1. As i want to refresh Page2 but Page2 doesn't display at all.

    How can i access my current page (Page2) after refreshing ?

    Thanks...
    Does your Page2 use some other GET parameters in its location string? I mean, is there a substring like
    _http://www.page2.htm?blah=1&blah2=2

    I tested the script on the localhost, but maybe a server you use has some additional options for redirection.
    I am still learning English

  • #12
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I can't use server side scripting. I just want to refresh html page so that css styles exist in that page are refreshed and reloaded from the server. I have used Meta tags for that purpose but they didn't work, that's why i am using Javascript for Refreshing Web page. Also i cant use PHP code. Anything related to HTML and JAVASCRIPT will be appreciated.


    Internet Marketing Company

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by Ariella Tyler View Post
    . I have used Meta tags for that purpose but they didn't work, that's why i am using Javascript for Refreshing Web page. Also i cant use PHP code. Anything related to HTML and JAVASCRIPT will be appreciated.
    Please re-read post #10.

    If that does not work, then nothing (client-side) will.

  • #14
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    farhanBajwa,
    I did my best to bring this script to work with any number of parmeters (GET) in the location string (like _http://www.page2.htm?blah=1&blah2=2)
    Code:
    <script type="text/javascript">
       function refreshMe() {
          var newLoc = "";
          var _now = new Date();
          var _prev = 0;
          var timeRe = /(&*)time=(\d+)(&|$)/;
          if (timeRe.exec(location.search)) {
             _prev = RegExp.$2;
             newLoc = location.href.replace(timeRe, RegExp.$1);
             var lastChar = newLoc.charAt(newLoc.length - 1);
             newLoc += ((lastChar == "&") || (lastChar == "?")) ? "time=" : "&time="
             if (parseInt(_prev) < _now.getTime() - 5000) {
                location.replace(newLoc + _now.getTime().toString());
             }
          } else {
             newLoc = (location.href.indexOf("?") == -1) ? location.href + "?time=" : location.href + "&time=";
             location.replace(newLoc + _now.getTime().toString());
          }
       }
       window.onload=refreshMe; /* there is no need to add an onload handler to body element */
    </script>
    Does it also redirect back to the Index page?
    I am still learning English

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Or simply raise an alert onload, with a cookie to expire in 60 seconds:-

    Please refresh your browser to make sure you get the latest version of this page.
    Last edited by Philip M; 08-13-2009 at 12:40 PM.


  •  

    Tags for this Thread

    Posting Permissions

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