Tuesday, July 28, 2009

How to solve Internet Explorer cache issue in Ajax


This is a frequent asked question which can be solved simply with a line of code with javascript.

The problem
If you use Internet Explorer you can find an annoying problem when you send multiple requests using Ajax. All new requests after the first request (using http.open method), mantain the same value of the first request. This problem is caused by Internet Explorer use of cache.

Solution

When you call the method:

http.open


add a random variable using Math object and add it in the method's URL argument, for example:

nocache = Math.random();
http.open('get', 'page.php?url_parameter='+parameter+'&nocache = '+nocache);


In this way all the new request will have an URL parameter (&nocache) which force the browser to "refresh" the old value with the new value.

Bookmark and Share
Digg this

No comments: