A cool AJAX trick to solve IE caching issue

IE loves to cache AjAX calls. If your JavaScript makes repeat calls to get new data, IE would not even bother to make that call and will just give your script a cached response. IE would not even honor no-cache headers for my AJAX requests. A popular trick that I learned from a friend and this discussion thread was to use a dummy parameter that will always different for each AJAX call. For example:

var d = new Date();
var myURL = "http://myserver:myport/myapp/myaction?d="+
d.getDate() + d.getHours() + d.getMinutes() + d.getMilliseconds();

Or you can use a simple counter variable.

About these ads

6 comments

  1. The “random parameter” is an old webmaster trick indeed. We used as well to fetch dynamically generated images and javascript.

    Very simple but saved us from a lot of testing nightmare.

  2. The only thing that I have found to work is the following (this is Java but the idea still applies):

    response.setDateHeader( “Expires”, -1 );
    response.setHeader( “Cache-Control”, “private” );
    response.setHeader( “Last-Modified”, new Date().toString() );
    response.setHeader( “Pragma”, “no-cache” );
    response.setHeader( “Cache-Control”, “no-store, no-cache, must-revalidate” );

  3. I have used this tip couple of years ago. Not long time ago I was debugging a client’s PC for debugging, I noticed in IE temporary Internet folder thousands of same Ajax link with different dummy parameters. This greedy IE kept every click the user did cached.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s