4

I have a question that is related to this answer, $.getScript(filename)

Are dynamically loaded files cached by the browser?

If not, how can I force them to be?

Community
  • 1
  • 1
tjb
  • 10,430
  • 7
  • 61
  • 85

1 Answers1

4

It seems that they are not. Proposed workaround is to redefine the function:

$.getScript = function(url, callback, cache) {
    $.ajax({
        type: "GET",
        url: url,
        success: callback,
        dataType: "script",
        cache: cache
    });
};

which could be used like this:

$.getScript('/foo.js', function() { }, true);
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • Thank you for finding this for me. The jQuery documentation for cache (http://api.jquery.com/jQuery.ajax/) says only that if you set cache to false the browser will not cache. It doesn't say that if you set it to true it will cache...off to do some testing – tjb Jan 22 '11 at 16:13
  • setting `cahce: true/false` via `.ajaxSetup` method also affects the `getScript`. – techie_28 Jul 13 '18 at 06:39