views:

637

answers:

1

I am using jQuery.ajax (http://docs.jquery.com/Ajax/jQuery.ajax) to inject the contents of a different web page into the current page. Think of this as a "preview" window. jQuery has an optional cache argument which works great at loading the contents from the cache instead of requesting the same page again.

My problem is that the contents that are being injected can be edited from a different location. When this happens, I want to invalidate the cache on the browser so that the next time the content of the page is requested, jQuery will request the page instead of using the cache. I could set the cache argument to false but then no content will be ever cached. I need content to be cached and only re-requested when the source content has been changed.

How can I invalidate the currently cached web page?

+2  A: 

In my opinon this is not possible. Assume that you call update.php using the AJAX call. Now everytime you want to use the cached version. But once in a while you want the uncached version. You have to inform your site to use the uncached version. Thus, you may need another parallel script which checks if ajax should fetch the cached or uncached version of update.php which basically defeats your purpose.

The way you can achieve this I think is using E-tags:

http://en.wikipedia.org/wiki/HTTP_ETag

Alec Smart
Does this mean that there is no way to access the list of cached items and simply remove an item from the list? Instead of checking to see if content has changed, when content is edited, I could just remove the page entirely from the cache...if it were possible.
Alison
To fetch the latest version everytime you can set the cache: false parameter in $.ajax function.
Alec Smart
You can use E-tags for that purpose- http://www.htaccesselite.com/creating-etag-headers-php-vt112.html
Alec Smart
http://www.weberdev.com/Manuals/PHP/function.http-cache-etag.html
Alec Smart