Ajax caching the webpage?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redbrad0
    New Member
    • Dec 2007
    • 3

    Ajax caching the webpage?

    I have a ajax script which calls the content of another webpage and just displays the content in a alert (its just the unix timestamp). Even though the time is changing, ajax does not report the new time until I load the page in my browser. How can I keep this so it does not cache the page? I need it to load a new version everytime.

    Main Page - test.php
    Code:
    <SCRIPT language=JavaScript type=text/javascript>
    
    function createXHR() 
    {
        var request = false;
            try {
                request = new ActiveXObject('Msxml2.XMLHTTP');
            }
            catch (err2) {
                try {
                    request = new ActiveXObject('Microsoft.XMLHTTP');
                }
                catch (err3) {
    		try {
    			request = new XMLHttpRequest();
    		}
    		catch (err1) 
    		{
    			request = false;
    		}
                }
            }
        return request;
    }
    
    function loadHTML(theURL)
    {
    	var xhr = createXHR();
    	xhr.onreadystatechange=function()
    	{ 
    		if(xhr.readyState == 4)
    		{
    			if(xhr.status == 200)
    			{
    				SessionTimeout = xhr.responseText;
    			}
    		} 
    	}; 
    
    	xhr.open("GET", theURL , true);
    	xhr.send(null);
    	
    	alert(SessionTimeout);
    }
    
    var SessionTimeout = "";
    </SCRIPT>
    
    <INPUT type="BUTTON" value="Read Timestamp" ONCLICK="loadHTML('test2.php')"></p>
    Requested Page - test2.php
    Code:
    <?
    echo time();
    ?>
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Try this..
    [CODE=javascript]
    <SCRIPT language=JavaSc ript type=text/javascript>
    function createXHR()
    {
    var request = false;
    try {
    request = new ActiveXObject(' Msxml2.XMLHTTP' );
    }
    catch (err2) {
    try {
    request = new ActiveXObject(' Microsoft.XMLHT TP');
    }
    catch (err3) {
    try {
    request = new XMLHttpRequest( );
    }
    catch (err1)
    {
    request = false;
    }
    }
    }
    return request;
    }

    function loadHTML(theURL )
    {
    var xhr = createXHR();
    xhr.onreadystat echange=functio n()
    {
    if(xhr.readySta te == 4)
    {
    if(xhr.status == 200)
    {
    alert(xhr.respo nseText);
    }
    }
    };

    xhr.open("GET", theURL , true);
    xhr.send(null);
    }

    </SCRIPT>
    [/CODE]

    Tell me if it works, else I'll tell you another solution.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Use the date/time to make the URL unique to force a new page, e.g. theURL+"?t="+(n ew Date()).getTime ().

      Comment

      Working...