SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJax cashes the webpages ... realy a problem !!

    hello guys,

    I am using an Ajax code to load dynamic content in a web page ... the problem is that the content changes every 3 seconds and I want the code to load the new content when a user clicks on a hyper link , but the Ajax code loads the cashed page instead of the new one .

    the request link :
    Code:
    <a href="#" onclick="open_url('operations.php','content');">Load the new content </a>
    and the Ajax code included in the header :

    Code:
    var xmlhttp=false;
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
     	try {
     		 xmlhttp = new XMLHttpRequest();
     	} catch (e) {
     		 xmlhttp=false;
     	}
    }
    
    
    
    if (!xmlhttp){
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    }
    
    
    
    if (!xmlhttp && window.createRequest) {
    	try {
    		xmlhttp = window.createRequest();
    	} catch (e) {
    		xmlhttp=false;
    	}
    }
    
    
    
    
    var please_wait = "Loading...";
    
    function open_url(url, targetId) {
      if(!xmlhttp)return false;
        var e=document.getElementById(targetId);if(!e)return false;
        if(please_wait)e.innerHTML = please_wait;
        xmlhttp.open("GET", url, true);
        xmlhttp.onreadystatechange = function() { response(url, e); }
        try{
          xmlhttp.send(null);
        }catch(l){
        while(e.firstChild)e.removeChild(e.firstChild);
    
        e.appendChild(document.createTextNode("Sorry,request failed .."));
      }
    }
    
    
    function response(url, e) {
      if(xmlhttp.readyState != 4)return;
        var tmp= (xmlhttp.status == 200 || xmlhttp.status == 0) ? xmlhttp.responseText : "Ooops!! A broken link!";
        var d=document.createElement("div");
        d.innerHTML=tmp;
        setTimeout(function(){
          while(e.firstChild)e.removeChild(e.firstChild);
          e.appendChild(d);
        },10)
    }

    I would be thankful , if any one could help ..

    thank you

    Almasry

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using POST instead of GET requests. Or maybe set an Expires header?

  3. #3
    SitePoint Zealot alfred3x's Avatar
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not AJAX that's doing the caching, but the browser. To get around that, you can add a random parameter to your URL. For instance:

    myajaxscript.php?r=2874692864

    You can use the time, put it through md5(), and take the last 5 or 6 characters.

    I think this is an IE issue.

    - AAA
    - A. Alfred Ayache
    PHP/MySQL ZCE

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not AJAX that's doing the caching, but the browser. To get around that, you can add a random parameter to your URL. For instance:

    myajaxscript.php?r=2874692864
    yes , i solves the problem , but in another way , using random value in the request function, something like this :

    open_url('operations.sma?do=mainview&date='+Date.parse(Date()),'content')
    thank you guys , i am realy thankful .


Bookmarks

Posting Permissions

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