views:

134

answers:

2

In IE using Javascript, is there way to know if doing a regular GET on a URL using XmlHttpRequest will pull its data from the cache or hit the server?

If there is no way to know in advance, is there a way to tell XmlHttpRequest to NOT hit the server, and instead pull from the cache if possible?

To clarify: I understand how browser caching works, I'm trying to understand if I can use javascript to determine in advance if an ajax call will use the browsers cache. In my case, I'm writing a JS library, so I don't control the server's behavior, so I'm highly constrained in terms of knowing apriori what cache settings/headers are.

+1  A: 

It's actually what decent HTTP user agents do by default: they fetch from the server only if there is no valid cache entry for the location that you request. So your best bet to control (or at least understand) caching behaviour is to look at the HTTP cache headers the web server returns for the location you request in your script.

Jan Krüger
I'm providing a 3rd party JS lib which needs to get at response.text w/o hitting the server (want to avoid side-effects since it's not my server/app). In order to do this, I would make a 'dummy' Ajax call if I'm confident it won't get passed the cache. So the question remains, is it possible to know if the browser has a URL in cache, and will use it? Otherwise, my code must not make the ajax call on behalf of the host page.
Aaron
Also, I'm new to StackOverflow, but my suspicion is that because this answer has an up-vote, I'm far less likely to get more eyeballs. Any chance you could down vote this in my quest to see if this can be determined via JS? :)
Aaron
I don't know of any API that allows you to determine what the browser has cached, sorry. As long as you don't approve my answer as the one you consider best, your question will keep on being listed in "Unanswered". I don't think it makes much of a difference anyway, since by now your question is probably way down the list already.
Jan Krüger
A: 

It seems to me you can never know for sure if something is cached, because even if it is at one point, it may be invalidated immediately afterwards. What you can do is forcing the browser to hit the server by adding a random querystring parameter to your request.

Manu