0

Possible Duplicate:
How to detect when XHR returns a cached resource?

I built an auto loader that only loads a javascript file if it has not already been loaded on this page using jquery with:

$.getScript();

I first check if the functions exist and if they do not I load them. However, this is unable to account for the browsers cache, I was hoping I might be able to figure out how.

One thing I would like to add to it is to see if the user already has the file saved in their browsers cache. Is it possible to check if a file from your server has already been cached on the users browser, so that I can skip making the call to the server at all?

Community
  • 1
  • 1
Case
  • 4,244
  • 5
  • 35
  • 53
  • 1
    If it's already in the browser cache then unless you manage the cache with etags there won't be a trip to the server when you try to load it. – Pointy Oct 14 '12 at 13:44
  • Right - it issues an HTTP GET, but the browser won't actually do any network operation if the URL is already in the cache. That's the whole point of the cache, after all. – Pointy Oct 14 '12 at 13:46
  • check out [this](http://stackoverflow.com/questions/12862247/check-if-preload-script-has-already-run-elements-are-in-cache/12862989) question – Asad Saeeduddin Oct 14 '12 at 13:46
  • Not a duplicate, this is specifically using getScript and how getScript does not cache unless the function is redefined. – Case Oct 14 '12 at 14:03

2 Answers2

3

The cache works transparently. If you request a file and the file exists in the user's cache, then the cached file will be used and there will not be an additional request to the server. The browser takes care of that, so you don't have to worry about it for the case that you're describing.

.getScript by default disables this using timestamped query arguments, so you simply need to disable the cache bypass which getScript uses:

$.ajaxSetup({
  cache: true
});
Matt Whipple
  • 7,034
  • 1
  • 23
  • 34
  • That's simply not the case. If you call getScript it always makes a call to the server to get the file. – Case Oct 14 '12 at 13:48
  • This did not work, however before I can formulate an appropriate response I need to test this more thoroughly. Thank you for your help. I will get back to this shortly with more information. – Case Oct 14 '12 at 13:52
  • This answer does not work, the only way to generate caching with getScript is to redefine the function. http://jamiethompson.co.uk/web/2008/07/21/jquerygetscript-does-not-cache/ – Case Oct 14 '12 at 14:00
  • You should be able to view this by checking on the requests made using the browser developer tools. If the cache is being bypassed then there should be some cruft added to the end of the script URL to ensure that it remains unique. If the URL requested is consistent and the cache is still not being used, then there may be an issue with a cache pragma or something similar. – Matt Whipple Oct 14 '12 at 14:00
  • That's a very old post, you'd be better off referring to the jQuery docs which also offer a similar solution in addition to the setting: http://api.jquery.com/jQuery.getScript/#caching-requests – Matt Whipple Oct 14 '12 at 14:07
1

Browser will determine whether to retrieve from server or not when request is made based on whether file is already in cache. jQuery will ad a timestamp so a new version is loaded each time. This can be turned off by setting cache:true

As for methods to check if you have already loaded in page so as not to load it again within your code you can do a variety of things.

  1. Add a class to element or body when script loads and check if class exists before making a new call.
  2. Set a variable flag withing the script and check for existence before making new call
  3. Check if a function within the script exists before making a new call
charlietfl
  • 170,828
  • 13
  • 121
  • 150