davidlee
davidlee

Reputation: 6167

how to detect external js file is already being included using jquery

I have included external js file using getScript. However, I would like to include it only once. How can i detect whether the external file has been included using jquery? Thanks.

Upvotes: 0

Views: 778

Answers (1)

brad
brad

Reputation: 32345

this doesn't technically check if the file itself has been loaded, but you can check for an object namespace defined within that file.

Say the JS file namespaced its code using something like:

var myNameSpace = function(){
  return {
   some:'stuff'
  };
}();

Or any style for namespacing something (ie jQuery does the same thing) then you can check for the presence of myNameSpace

So

if (typeof myNameSpace == "undefined"){ // load script }

Upvotes: 1

Related Questions