Browser caching is generally a good thing, why download the same image again and again while you can download it once and save it on the browser? In some cases, however, this is not a wanted behavior.
The problem:
AJAX application tends to frequently ask the server for the same URL (a stock quote, application status and so forth). The AJAX application expect a response form the server while, in fact, after the first request the browser returns the cached page and the AJAX application does not work as expected. Clearing the browser cache or setting “no cache” parameters on the page does not always work.
The solution:
Add a pseudo-random parameter to the URL the AJAX application is requesting. Doing so will fool the browser to think this is a new page that has not yet been cached.
For example, this is how I would add a pseudo parameter to a URL in JavaScript :
var url = “http://mydomain?myParameters=myValues&pseudoParam= “+new Date().getTime();
The Date().getTime(); returns a new value every millisecond (or so), the browser thinks this is a new page that has not been cached and requests the URL form the server.
This will hopefully work for flash and image assets that are regularly updated as well?
Thanks,
This should work fine for all media types.
Cheers
Amir Shevat
OOOw
Tha is a very simple but stronge solution
Tks
$(document).ready(function() { $.ajaxSetup({ cache: false }); });
jQuery also uses a random value of the URL.
Check here: http://api.jquery.com/jQuery.ajax/
It says: Setting cache to false also appends a query string parameter, “_=[TIMESTAMP]”, to the URL.
but this trick didn’t work for iPanel browser keeps caching every page, but works fine in other browsers
I have been implementing Ajax on some of my pages…this simple solution works better than adding the parameters in the headers.
thanks!
Thanks! The solution worked great however I do have one concern. Does this mean my website viewers will be collecting large amounts of cached XML pages? The random number added to the URL does ensure the newest content is available but that doesn’t prevent each instance from being cached, correct?
Thanks Amir. This was exactly what I needed.
I loved this solution, it worked perfect xD xD.
Response.CacheControl = “no-cache”;
Response.AddHeader(“Pragma”, “no-cache”);
Response.Expires = -1;
Using this:
Response.CacheControl = “no-cache”;
Response.AddHeader(“Pragma”, “no-cache”);
Response.Expires = -1;
Does not work in old browsers….
I found that a pseudo-random parameter works better.
Worked like a charm. But is it a professional approach? I am not sure!! But solves my problem exactly… Thanks
Thanx a lot for sharing..helped a lot
Hey guys, just wanted to add a way to ad this random string to the URL. When passing the url you can append the random function Math.random(); to the url string like this:
var urlVar = urlVar + ‘&’ + Math.random();
This will solve the problem also.
Good Luck!
you could also use Data
…
var t = “” + new Date().getTime();
httpRequest.open(“POST”, “/someurl?t=”+t, false);
…