Null Disquisition

In Mother Russia, Thesis writes You!

From the Archives: Ajax IE Caching Issue - Recap

without comments

Author’s Note:
Here is another article from my long lost blog. Ajaxian still links in here, as well as many other personal blogs. See my previous post for background.

-David

Post:
In light of the comments on the original post, i thought it would be nice to reiterate some other people’s workarounds.

This was the first one brought to my attention, and actually is how i work around the caching these days. Basically, you add a superfluous variable to your string of GET vars that is unique - timestamp works fine. I actually go a little bit further and use milliseconds since the app i’m working on makes several requests per second.

servlet/imagemaker.jsp?foo=bar&goo=car
Change to:
servlet/imagemaker.jsp?foo=bar&goo=car&time=111111111

Here is function to uncache the url:

function uncache(url){
var d = new Date();
var time = d.getTime();
return url + ‘&time=’+time;
}

This workaround looks like as close to a valid solution as there is. It simply adds an arbitrary variable to the POST vars (sent in the send method).

http.open(’post’, ‘myfile.php’);
http.setRequestHeader(’Content-Type’, ‘application/x-www-form-urlencoded’);
http.onreadystatechange = handleResponse;
http.send(’var=1′);

A null or empty string for the send method will cause IE to cache.

My only recommendation for that second solution would be to change the POST var to send(’ie=teh_suck’);

Kudos to contributors.

Comment Thread:

Sujai Said…

What about xml files. I am trying to call xml using ajax. I tried POST its not working

  • September 14th, 2006 at 11:57 pm
Steve Said…

Thanks for this, I had already spent far too long trying to work out why ie was caching the records (thought it may have been a bug in ie7). For those that are interested below is a script i used the above suggestion with, it uses a php loop to change the field which it will output the fetched data to.
cheers

function getContent(){
var page=’./query.php’;
var selected=document.getElementById(’selected’).value;

if (selected==’’){selected=’none’;}

//params has to have following format
//i.e.: c=1&id=3….

//Clear our fetching variable

var xmlhttp=false;

//Try to create active x object
try {
xmlhttp = new ActiveXObject(’Msxml2.XMLHTTP’);
} catch (e) {

try {
xmlhttp = new ActiveXObject(’Microsoft.XMLHTTP’);
} catch (E) {
xmlhttp = false;
}

}

if (!xmlhttp && typeof XMLHttpRequest!=’undefined’) {
alert(’Error’);
}

var rowNum=’’;

xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4 && xmlhttp.status== 200) {
document.getElementById(’qty’).innerHTML = xmlhttp.responseText;

}
}

params=’selected=’+ selected +’&rownum=’+ rowNum +’&rnd=’+ Math.random()*99999;

xmlhttp.open(’POST’, ‘./query.php’, false);

xmlhttp.setRequestHeader(’Content-type’, ‘application/x-www-form-urlencoded’);

xmlhttp.send(params);

}

  • October 25th, 2006 at 6:56 pm
Chris Said…

Glad I have found this article, bookmarking for work =D

We plan on doing an entirely new website (hopefully I can get them to separate html/css/back-end) this time around as well as get some ajax in there to make life a bit easier!

  • November 14th, 2006 at 7:41 pm
Mark Said…

Excellent. Thanks for this.

Been banging our heads against the proverbial Brick 2.0 Walls trying to work out why our Ajax looping updates don’t update in IE7.

  • December 13th, 2006 at 1:42 pm
Gareth Said…

That worked a treat.

I’m still a bit annoyed that I have to do this for every request. Such is the life we lead.

  • December 18th, 2006 at 3:43 pm
Gene Said…

Wow, thank you VERY much. I’ve been having this problem for some time now. This little line “req.send(’var=1′);” really helped me out. Thanks VERY VERY much.

  • December 20th, 2006 at 6:56 pm

Written by david

July 2nd, 2008 at 12:12 am

Posted in archives

Tagged with , , , , ,