rolling stone
rolling stone

Reputation: 645

How to ensure that the javascript is loaded only once

I am calling a JS using jquery getScript().

Sometimes i could see that the files are already loaded (cached resource). So,On refreshing the cached page is not removed and also the same file is loaded again.

Because of the multiple includes of the same file i am getting errors.

How to avoid that ?

$.getScript("http://localhost:8888//../../demo.js", function() { console.log('Script is loaded.'); });

Upvotes: 0

Views: 5861

Answers (4)

Ajit Kumar KV
Ajit Kumar KV

Reputation: 1

Cashingvis good feature I solved multiple time loading js when I I load through jquery before. My issue was when I call a file loading by jquery I have a jquery file in that loading file now it loads only once so then events i now envoje only once. Thanks a lot have a nice day.

Upvotes: -2

Skylar Challand
Skylar Challand

Reputation: 31

Assuming your demo.js file contains at least one function or variable, you could check for presence before loading again:

if (typeof(your_variable) === "undefined") {
  $.getScript("http://localhost:8888//../../demo.js", function() { console.log('Script is loaded.'); });
}

(where your_variable is the name of a function or variable inside demo.js)

Upvotes: 1

Evan
Evan

Reputation: 6115

By default, $.getScript sets the cache setting to false. Try setting it to true to see if this solves your problem:

$.ajaxSetup({
  cache: true
});

Add the above before your call like:

$.ajaxSetup({
      cache: true
    });

$.getScript("http://localhost:8888//../../demo.js", function() { console.log('Script is loaded.'); });

directly from jquery docs:

Caching Responses

By default, $.getScript() sets the cache setting to false. This appends a timestamped query parameter to the request URL to ensure that the browser downloads the script each time it is requested. You can override this feature by setting the cache property globally using $.ajaxSetup():

$.ajaxSetup({ cache: true }); Alternatively, you could define a new method that uses the more flexible $.ajax() method.

Examples: Example: Define a $.cachedScript() method that allows fetching a cached script:

jQuery.cachedScript = function( url, options ) { // Allow user to set any option except for dataType, cache, and url options = $.extend( options || {}, { dataType: "script", cache: true, url: url }); // Use $.ajax() since it is more flexible than $.getScript // Return the jqXHR object so we can chain callbacks return jQuery.ajax( options ); }; // Usage $.cachedScript( "ajax/test.js" ).done(function( script, textStatus ) { console.log( textStatus ); });

Upvotes: 4

dezman
dezman

Reputation: 19358

I believe if it is cached the browser will not go make a new request for it, it will know to load the cached version, so you are good just firing off your $.getScript as you have it.

It may appear in the network tab of chrome developer tools again, but the time will be 0 and the Size (Content) value will say '(from cache)' This would be a good way to test what is actually going on.

Upvotes: 1

Related Questions