欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

动态加载外部javascript文件的函数代码分享_javascript技巧

程序员文章站 2022-03-19 10:01:52
...
复制代码 代码如下:

(function (clover) {
clover.loadScript = function loadScript(url, callback) {
var heads = document.getElementsByTagName('head');
if (heads.length == 0) {
alert("page must have one head element");
}
var head = heads[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// most browsers
script.onload = callback;
// IE 6 & 7
script.onreadystatechange = function () {
if (this.readyState == 'complete') {
callback();
}
}
head.appendChild(script);
}

})(window.clover = window.clover || {});

// sample
// clover.loadScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");
相关标签: 加载外部