by Daniel | hits(15375)
TAGS: trick jQuery Ajax javascript web 2.0 Internet Explorer cache
As you probably know the Internet Explorer family is greedy for caching!
This is not particularly pleasant if you don't know that this peculiarity also applies to Ajax GET requests!
Strictly speacking IE caches Ajax GET requests, therefore making all your web2.0 tricks effortless!
My solution in roder to hovercome this problem is to add a GET parameter, a timestamp, in the Ajax GET requests.
The only purpose of this parameter is to make the request unique in IE head!
This is the snippet I use:
//lreturns a timestamp. Used to avoid IE caching Ajax requests
function myTimestamp(){
tstmp = new Date();
return tstmp.getTime();
}
Then you add it to your AjaxGET request as follows (I use jQuery in this example):
BASIC example
jQuery('#myDIV').load('/myurl.php?avoidcahe=' + myTimestamp());
CakePHP example:
jQuery('#myDIV').load('/myurl/param_1/param_2/' + myTimestamp());
Check out the demo, with Internet Explorer and another browser to see the difference!
By the way, if you still stick with Internet Explorer, please get a better surfing experience, there is plenty of alternatives:
Personally, my favourite is Firefox!
Another solution is to just use Ajax POST requests (jQuery.post())!
Happy coding!
Dan
view/hide comments | add comment
2010-05-05 17:05:27
You just saved my life and job. Thanks a million for the lovely tip.
[And f*ck IE]
2010-06-22 11:51:06
Can I use that image?
2011-04-09 23:20:33
I have been banging my head against the wall with this for hours!!! Thank you!!! Can I take a freakin' break now? And ditto on the comment about ie!
2011-05-26 14:22:38
It is easier to disable caching in global AJAX options:
$.ajaxSetup({
cache: false
});
...and all future AJAX-based GET-requests (including $.getJSON) will have a random number applied to the URL.
2011-09-16 11:29:46
Thanks a million!!!
2011-10-31 13:10:28
Brilliant! This issue sucks as on Firefox works fine! That's a bad heck but has saved me a lot of time! Thanks!
2011-12-03 19:05:09
I have added this information on my blog "Coming Soon Live""
2011-12-06 10:11:38
Another question is how i can disable jquery timestamp on start ?
powered by 4webby.com