前端异步加载js且判断js是不是加载完成
展开全部
看看我这个方法行不行,异步加载JS,加载完成后执行JS。
function dynamicLoad( url, callback) {
var _doc = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url);
_doc.appendChild(script);
script.onload = script.onreadystatechange = function () {
if (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') {
callback();
}
}
}
function dynamicLoad( url, callback) {
var _doc = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url);
_doc.appendChild(script);
script.onload = script.onreadystatechange = function () {
if (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') {
callback();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询