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

代码如下:

        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回调.

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

posted @ 2015-05-26 15:58  诗&远方  阅读(10394)  评论(0编辑  收藏  举报