6

I have an php powered application with javascript and many jax calls. my application is working upto date in firefox. but when i run it in internet explorer-8 or similar versions my ajax call gets cached in my browser so i am not able to output the upto date info with the ajax calls instead the result for that ajax calls are served with old data's which reside in the browser cache.

 I have tried lots of possible options as listed below

1.) I added following meta tag in header files


<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>

2.)I added Following php code

header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');

But still the above 2 approaches did not solve my problem ie, please can anybody help me to disable caching internet explorer when my application runs, so that its possible to get upto date information.

thanks in advance

gowtham kumar
  • 227
  • 5
  • 10
  • The second solution with HTTP headers should work. Just make sure the headers are really being sent (use web-sniffer.net), clear the browser's cache (files were probably cached when there were no headers sent) and check it once again. – duri Aug 05 '11 at 09:40
  • *I added Following php code* -- in which file? The response headers should go in your AJAX script. – Salman A Aug 05 '11 at 10:09
  • @duri I don't believe that solution will work if an item is already in cache. I think you might be able to hard refresh using javascript => http://stackoverflow.com/questions/2099201/javascript-hard-refresh-of-current-page/2099205#2099205. The rest of the items I think you should version using something like filemtime() – Alfred Aug 05 '11 at 14:07
  • @Alfred I'm aware it might not work if files were already cached. This is why I recommended clearing the browser's cache. – duri Aug 05 '11 at 14:44

1 Answers1

9

Make each AJAX request unique in some way. That will prevent IE from caching the response.

For example, if your normal AJAX query URL is www.mysite.com/ajax.php?dog=cat, add in a querystring parameter to each AJAX request that is unique:

www.mysite.com/ajax.php?dog=cat&queryid=1

Increment that parameter each time you make an AJAX request, and that should hopefully do the trick for you.

James
  • 13,092
  • 1
  • 17
  • 19
  • ya i have done each ajax request unique by adding the current time with seconds but i am searching for a better way to get a solution generic for all my files and all requests can anybody please help me. – gowtham kumar Aug 05 '11 at 09:42
  • 1
    `url + "&rand="+Math.floor(Math.random()*11)` – Lawrence Cherone Aug 05 '11 at 09:46
  • Why use up processor time by using Math.random? A simple increment will do the trick and isn't as wasteful. – James Aug 05 '11 at 10:09
  • gowtham - I can't think of a more simple solution than simply appending an incremented int each time you make a request. Many Javascript libraries do something similar. – James Aug 05 '11 at 10:14
  • use filemtime() => http://php.net/manual/en/function.filemtime.php. incrementing is also expensive because you need to keep track of counter via database. filemtime() is just simple system call. The best would be even to do this once at file creation! – Alfred Aug 05 '11 at 10:26
  • You don't need to keep track of the counter via the database, it hasn't got anything to do with the database. It is purely there to fool IE into thinking the HTTP request is unique to the last one. Incrementing a integer is one of the least expensive operations I can think of. – James Aug 18 '11 at 15:54
  • I am facing same problem, can u give me the solution? cache problem occurs on the ajax link only in IE. i will b very thank ful to u – Usman Ali Jan 24 '13 at 07:49
  • The solution is in the answer - make each AJAX request unique in some way, otherwise IE will cache the response each time. – James Jan 25 '13 at 14:30
  • Using `Math.random()` worked for me (without using `Math.floor()` or anything else). I can't increment because sometimes IE holds onto the cache between page loads (which erase my local variables), so I need a method of generating unique strings that will work between page loads. I [detected IE using `!!document.documentMode`](http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser) so the performance cost only effects IE, which people shouldn't be using anyway if they want a streamlined online experience. – intcreator Jul 28 '16 at 21:11
  • LoL that was exactly what I said to one of our FE engineers, but laughs aside, it's sad that there isn't a better solution for this issue... – Nir Alfasi Feb 01 '19 at 00:57