function loadScript(src, onLoad){
let scriptTag = document.createElement('script');
scriptTag.src = src;
if(typeof onLoad === 'function'){
scriptTag.onload = onLoad;
// -----start------ //
scriptTag.onreadystatechange = function(){
if(scriptTag.readystate === 4){
onLoad();
}
}
// -----end------ //
}
document.body.appendChild(scriptTag);
}
loadScript('app.js', function(){
alert('script is ready');
});
请问一下,注释包围的部分有必要写吗,不写好像也会执行 alert
注释里面的代码,用
onreadystatechange
主要为了兼容IE。动态创建script标签可以看这篇文章Loading JavaScript without blocking