0

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.

3
  • even if you insert the same file twice , the browser will take care of you ,it will download the file only once... Commented Dec 1, 2010 at 17:35
  • @gov Download once, but would it execute it twice? Commented Dec 1, 2010 at 17:38
  • @gov and @orbling, yes indeed it execute numbe of times the function is run. for example, if i have $(function(){ $('#btn').live('click', function(){alert('');) }); if getscript is execute twice, the alert box will come out twice when button is clicked. I would like it to comes out only once.. Commented Dec 1, 2010 at 17:47

1 Answer 1

1

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 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.