Disable Cache in jQuery

I usually run into this problem when dealing with good ol’ Internet Explorer (..any version of IE actually).

The problem is that IE tries to be smart and not tell you the new output of a file it fetches more than one time with the same filename, instead it shows you what it saw the first time it loaded that file.

You can imagine this would be insanely dumb if you were using a realtime application data source where a script output a different resultset each time, e.g. most database ajax requests.

So what you need to do is just disable caching in jQuery.

$.ajaxSetup ({ cache: false });
Code language: JavaScript (javascript)

You don’t have to add this inside your document ready function but I like to incase the jQuery library isn’t finished loading yet for any particular reason, so if the above throws a warning/error then do this:

$(function() { $.ajaxSetup ({ cache: false }); });
Code language: JavaScript (javascript)

Essentially what this does is it adds a random number to the request uri as a get variable, by this being different each time it forces the browser to return the new output. e.g:

Notify of
Inline Feedbacks
View all comments