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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ajax IE caching nightmare

    Hi,

    I'm having trouble with an Ajax script that auto refreshes content on a page. The code worked fine on FireFox, and then i found out that it wont work on either IE6 or 7. I tested further on Opera (didn't work) and Safari (did work).

    I've been doing some reading about this issue and found that apart from caching via headers some people pass a random number as a variable in the url which is supposed to solve the problem. surprisingly (or not) it fixed the problem on Opera, but not on either version of IE.

    The script just loads a time stamp from a file called time.php

    Im attaching the code in hope someone might put out me of my misery

    Code:
    <?php
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    ?>
    
    <head>
    <script type="text/javascript">
    var randomnumber = Math.floor(Math.random()*1000001);
    var page = "/4xp/ajax/time.php?dummy=" + randomnumber;
    
    function ajax(url,target)
     {
     
        // native XMLHttpRequest object
       document.getElementById(target).innerHTML = 'sending...';
       if (window.XMLHttpRequest) {
           req = new XMLHttpRequest();
           req.onreadystatechange = function() {ajaxDone(target);};
           req.open("GET", url, true);
           req.send(null);
       // IE/Windows ActiveX version
       } else if (window.ActiveXObject) {
           req = new ActiveXObject("Microsoft.XMLHTTP");
           if (req) {
               req.onreadystatechange = function() {ajaxDone(target);};
               req.open("GET", url, true);
               req.send(null);
           }
       }
    		   setTimeout("ajax(page,'scriptoutput')", 1000);
    }
    
    function ajaxDone(target) {
    // only if req is "loaded"
    if (req.readyState == 4) {
    // only if "OK"
    if (req.status == 200 || req.status == 304) {
    results = req.responseText;
    document.getElementById(target).innerHTML = results;
    } else {
    document.getElementById(target).innerHTML="ajax error:\n" +
    req.statusText;
    }
    }
    }
    </script>
    </head>
    <body onload="ajax(page,'scriptoutput')">
    <p>Current Server date & time:<br />
    <span id="scriptoutput"></span></p>
    </body>
    and time.php is just:

    Code:
    <?php
    echo time();
    ?>
    Thanks!

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I used to use the random GET string hack, but now I've found setting a header with the XMLHttpRequest setRequestHeader() method is best:
    Code:
    /* xhr is your request object of course */
    xhr.open("GET","script.php",true);
    /* send If-Modified-Since header
    xhr.setRequestHeader("If-Modified-Since", "Fri, 31 Dec 1999 23:59:59 GMT");
    xhr.onreadystatechange= function() {
     /* callback function */
    }
    xhr.send(null);
    You shouldn't have to worry about any caching issues, the header ensures it always returns a result based on your If-Modified-Since header. Just make sure it's long enough in the past to work. I use "doomsday eve", AKA the night before Y2K.

  • The Following 2 Users Say Thank You to bdl For This Useful Post:

    Eris (07-26-2008), oesxyl (06-17-2008)

  • #3
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up

    I've searched so long how to solve this problem and finally found the way. This really works. Thx A LOT bdl.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    523
    Thanks
    26
    Thanked 0 Times in 0 Posts
    yes, fixed my problem too! kudos!
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #5
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There is a much easier option and you can remove all the caching headers at the top as well. This solved all my caching issues:

    xmlHttp.open('GET', url+'?bustcache='+new Date().getTime(), true);

  • #6
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    To bdl,

    Thank you for posting! You're the man

    Cheers


  •  

    Posting Permissions

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