Why people are still using Internet Explorer 6 (or any version for that matter) is beyond me; but recently when developing a trending application for a company that only supports IE6 I ran into the age-old caching issue. Normally, I would add a random number at the end of the URL string, however, this application has 30-40 different URLLoaders and that would just be annoying.
For those who may not be aware, when loading dynamic XML data in Flash or Flex, say from a PHP page, if you are using the same link over and over again, IE6 decides that it only needs to check the link once.
It seems the easiest way to prevent caching in Internet Explorer is to set the Headers on the response of your data to “Pragma: no-cache” and “Cache-Control: no-store”. Now this may be a little overkill, but for my problem, this was the only option. You could try and allow a little more caching by using “Pragma: public” but this didn’t work in my case.
I’m using Java to serve XML data, so my code looks something like this (where op is a servlet request)
-
op.getResponse().addHeader("Cache-Control", "no-store");
-
op.getResponse().addHeader("Pragma", "no-cache");
But in PHP you could do this:
Just remember, as always with the PHP Header() function, that you put this before anything is sent to the client – include any white space before your beginning <?php tag.
A great resource for more information (including a testing client) is located on the Less Rain Blog at http://www.blog.lessrain.com/flash-loading-and-browser-cache-test-suite/
I wrote similar class year ago. I cannot post it as it is property of my company. Anyway, I used this code from kirupa forum to avoid caching:
var loader:URLLoader = new URLLoader();
var header:URLRequestHeader = new URLRequestHeader(“pragma”, “no-cache”);
var request:URLRequest = new URLRequest(“data,xml”);
request.requestHeaders.push(header);
loader.load(request);
Kdr, I never realized that you were able to set those headers from the client/requester. I’ve always just set this server side, these seems a lot easier, especially if you don’t have access to server-side headers. Thanks for the reply.
This didn’t work for me when I was having problems creating an editible XML based image gallery in flex which reloads an edited XML:
var loader:URLLoader = new URLLoader();
var header:URLRequestHeader = new URLRequestHeader(”pragma”, “no-cache”);
var request:URLRequest = new URLRequest(”data,xml”);
request.requestHeaders.push(header);
loader.load(request);
But however this did work:
var xmlPath:String=”replaceYourXMLPathHere.xml”
var urlReq:URLRequest = new URLRequest(xmlPath+”?time=” + new Date().getTime());
from here: http://www.newtonflash.com/blog/2009/06/08/prevent-xml-caching-problem/#comment-43
atton – I haven’t tried Kdr’s suggestions, I was unaware of that solution – it looks like it may not work (at least in your code). Adding the time or a random number at the end of a URL is an easy hack – especially if you’re using an actual XML file and have no control over headers sent from the web server. If you’re dynamically generating the XML using PHP or Java or some other server side language it’s a little more elegant to set the headers to take care of caching.