首页 >web前端 >js教程 >动态加载外部javascript文件的函数代码分享_javascript技巧

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

WBOY
WBOY原创
2016-05-16 18:04:161159浏览
复制代码 代码如下:

(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");
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn