1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Ajax IE caching nightname

Discussion in 'JavaScript' started by Ballboy, Jun 16, 2008.

  1. #1
    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 :(

    <?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>
    Code (markup):
    and time.php is just:

    <?php
    echo time();
    ?>
    Code (markup):
    Thanks!
     
    Ballboy, Jun 16, 2008 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to put those headers in time.php, they are not needed in the ajax page.

    time.php:
    <?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");
    echo time();
    ?>
    PHP:
    It worked for me. :)
     
    MMJ, Jun 17, 2008 IP
  3. Ballboy

    Ballboy Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    nice one MMJ :D
     
    Ballboy, Jun 17, 2008 IP