开发者

Auto include javascript file if not already included

I have an external javascript file that relies on the presence of another file.

How can I, using JavaScript (or jQuery) automatica开发者_JS百科lly include this file if it is not already included (I can test based on the presence of a known function in this external file)

Edit: It now includes the file but writes over everything completey! I have tried all suggested methods put forward so far


Well if that JavaScript file defines a specific variable (or a function) you can check its presence by checking typeof that_variable, then load the JavaScript file if necessary. An example, here is how you load swfobject library if its not available on the page:

if (typeof swfobject == "undefined") {
   var e = document.createElement("script");
   e.src = "http://ajax.googleapis.com/ajax/libs/swfobject/2/swfobject.js";
   e.type = "text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e); 
}


http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜