实现一个loadScript(url,callback)用来判断一个js加载完成并且执行callback操作。
原理:document.readyState
function loadScript(url,callback){
var script=document.createElement(‘script‘);
script.type=‘text/javascript‘;
script.async=‘async‘;
script.src=url;
document.body.appendChild(script);
if(script.readyState){ //IE
script.onreadystatechange=function(){
if(script.readyState==‘complete‘||script.readyState==‘loaded‘){
script.onreadystatechange=null;
callback();
}
}
}else{ //非IE
script.onload=function(){callback();}
}
}