Forum Moderators: open

Message Too Old, No Replies

AJAX - IE object caching page - fix I found

IE object caches page instead of getting update

         

Double_Dark

5:59 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



It seems that a number of other people have had this problem and when I tried to solve it I had a hard time finding a fix so I thought that I would post my fix.

I'm using AJAX ala google suggest to grab a page to keep the visitor updated as to the status of a request. The problem I ran into is that in IE the object was caching the page that it was calling instead of retrieving the updated page.

The fix I found was to add a request header If-Modified-Since to the request such that it would be before the page was modified.

Here's the code.


var _xmlHttp=null;

//This will be the object to retrieve the page
function getXMLHTTP() {
var A=null;
try { A=new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e)
{
try
{ A=new ActiveXObject("Microsoft.XMLHTTP");}
catch(oc)
{ A=null; }
}
if(!A && typeof XMLHttpRequest!= "undefined") {
A=new XMLHttpRequest();
}
return A;
}

//Sets the content of B
function SetDiv() {
if(_xmlHttp.readyState==4 && _xmlHttp.responseText) {
while(B.childNodes.length>0) {
B.removeChild(B.childNodes[0]);
}
B.innerHTML = _xmlHttp.responseText;
}
}

//Requests whatever the page is and specifies where to put the contents
function getPage(the_page,the_div) {
B = document.getElementById(the_div);
_xmlHttp=null;
_xmlHttp=getXMLHTTP();
//I needed to keep track of the Cold Fusion session for which the page was being called
if(the_page.indexOf("?") >= 0) {
the_page = the_page + "&cfid="+getCookie('cfid')+"&cftoken="+getCookie('cftoken')
} else {
the_page = the_page + "?cfid="+getCookie('cfid')+"&cftoken="+getCookie('cftoken')
}
_xmlHttp.open("GET",the_page,true);
_xmlHttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
_xmlHttp.onreadystatechange = SetDiv;
_xmlHttp.send(null);
}

httpwebwitch

1:44 am on Aug 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



awesome. nice solution - this post is getting a bookmark