javascript动态创建script标签,加载完成后调用回调

时间:2022-09-07 18:05:01

代码如下:

        var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.charset = 'GBK';
script.onload = script.onreadystatechange = function() {
if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete" ) {
callback();
script.onload = script.onreadystatechange = null;
}
};
script.src= url;
head.appendChild(script);

这段代码实现了动态创建script标签,然后插入到head标签中.然后当js加载完毕的时候调用callback回调.

想解释点什么,但觉得实在没什么好说的...好吧...就这样吧...看看就行了...