Fixing the jQuery AJAX Page Caching Problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Fixing the jQuery AJAX Page Caching Problem

Post by volomike »

You may experience in certain browsers, or certain browser versions even, where you use jQuery's .load() event or other AJAX call to pull back a page from a server and load it into a DIV or something, and the page is cached instead of getting the latest copy. Of course, unfortunately headers are already sent when you do that, so you can't turn fix the page cache through a header() call in the PHP of that page. Turns out, this is a common problem. The fix is this:

Code: Select all

$().ready(function(){ //when page has fully loaded
  $.ajaxSetup ({
    // Disable caching of AJAX responses */
    cache: false
  });
});
 
You'd think that would be off by default with jQuery, but not so!
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Fixing the jQuery AJAX Page Caching Problem

Post by kaszu »

What 'cache' option does is adds timestamp to the url query, making browser think it's a different url.
You can fix it with headers by setting them for ajax response, not the document itself.
Post Reply