Ajax - Cache Busting with Javascript

Browsers are notorious at hanging on tenaciously to cached pages.  Even when you use all manner of fancy headers like "Pragma: no-cache" or "Cache-Control: must-revalidate" you'll often find that you receive a cached page rather than a 'live' one.  Here's something else you can try, which has worked well for me: 

To the URL which requests the page, add a random (and meaningless) variable and value pair.  The value should be different each time, and although it will have no effect on the called page, it helps to fool the browser into thinking it has to return a page from a URL it's never visited before, rather than realising it has it in cache.

A good way to do this in Javascript is to add a random number:

var myurl='http://www.mydomain.com/data.php'; myRand=parseInt(Math.random()*99999999); // cache buster window.open(myurl + "?rand=" + myRand,"mywindow","status=1,toolbar=1");

Now, each time we open the page whose address is stored in myurl, we append a meaningless variable rand with a random value, eg:

www.mydomain.com/data.php?rand=54321</FONT></P>


Before using any software from this site, please see the Terms



8 Users Online.

All of the code and information on this site is provided free of charge (but without warranty).
If you feel that the site has been useful, you may care to donate a little toward the running of the site and the development of further projects?