动态加载js并判断完成

function creatrScript(jsUrl,callback) {
    var _doc = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', jsUrl);
    _doc.appendChild(script);
    script.onload = script.onreadystatechange = function() {
        if (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') {
            callback();
        }
        script.onload = script.onreadystatechange = null;
    }
}

creatrScript("jsUrl",function(){
    alert("ok")
});

来源网络,稍作修改

你可能感兴趣的:(动态加载js并判断完成)