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.
-
even if you insert the same file twice , the browser will take care of you ,it will download the file only once...kobe– kobe2010-12-01 17:35:52 +00:00Commented Dec 1, 2010 at 17:35
-
@gov Download once, but would it execute it twice?Orbling– Orbling2010-12-01 17:38:11 +00:00Commented 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..davidlee– davidlee2010-12-01 17:47:47 +00:00Commented Dec 1, 2010 at 17:47
Add a comment
|
1 Answer
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 }