javascript 文件的同步加载与异步加载实现原理(2)


var loadJS = function(url,callback){
var head = document.getElementsByTagName('head');
if(head&&head.length){
head = head[0];
}else{
head = document.body;
}
var script = document.createElement('script');
script.src = url;
script.type = "text/javascript";
head.appendChild( script);
script.onload = script.onreadystatechange = function(){
//script 标签,IE 下有 onreadystatechange 事件, w3c 标准有 onload 事件
//这些 readyState 是针对IE8及以下的,W3C 标准的 script 标签没有 onreadystatechange 和 this.readyState ,
//文件加载不成功 onload 不会执行,
//(!this.readyState) 是针对 W3C标准的, IE 9 也支持 W3C标准的 onload
if ((!this.readyState) || this.readyState == "complete" || this.readyState == "loaded" ){
callback();
}
}//end onreadystatechange
}


对于第4点的测试(同步加载)(其中插入 alert 很容易看到加载时的阻塞)
tryjs.html

复制代码 代码如下:


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://www.jb51.net/tryjs.js"
onload="if(!document.all){console.log('outer js callback, not IE');}"
onreadystatechange="console.log('outer js callback ',this.readyState,' IE');"></script>
<body>
</body>
</html>


tryjs.js

复制代码 代码如下:


console.log('write begin');
document.write('<script src="https://www.jb51.net/try.1.js" onreadystatechange="console.log(\'file 1 callback \',this.readyState,\' IE\');"><\/script>');
document.write('<script src="https://www.jb51.net/try.2.js" onreadystatechange="console.log(\'file 2 callback \',this.readyState,\' IE\');"><\/script>');
console.log('write finished');


try.1.js

复制代码 代码如下:


console.log('loadjs 1 begin');
console.log('loadjs 1 finished');


try.2.js

复制代码 代码如下:


console.log('loadjs 2 begin');
console.log('loadjs 2 finished');


测试结果(file 2 和 file 1 的 callback complete 在IE7\8\9次序不确定)
IE 7:
日志: outer js callback loading IE
日志: outer js callback loaded IE
日志: write begin
日志: write finished
日志: outer js callback complete IE
日志: file 1 callback loading IE
日志: file 2 callback loading IE
日志: loadjs 1 begin
日志: loadjs 1 finished
日志: loadjs 2 begin
日志: loadjs 2 finished
日志: file 2 callback complete IE
日志: file 1 callback complete IE

IE8:
日志: outer js callback loading IE
日志: outer js callback loaded IE
日志: write begin
日志: write finished
日志: outer js callback complete IE
日志: file 1 callback loading IE
日志: file 2 callback loading IE
日志: loadjs 1 begin
日志: loadjs 1 finished
日志: loadjs 2 begin
日志: loadjs 2 finished
日志: file 2 callback complete IE
日志: file 1 callback complete IE

IE9:
日志: write begin
日志: write finished
日志: outer js callback complete IE
日志: file 1 callback loading IE
日志: file 2 callback loading IE
日志: loadjs 1 begin
日志: loadjs 1 finished
日志: loadjs 2 begin
日志: loadjs 2 finished
日志: file 1 callback complete IE
日志: file 2 callback complete IE

FIREFOX:
write begin
write finished
outer js callback, not IE
loadjs 1 begin
loadjs 1 finished
file 1 callback,NOT IE
loadjs 2 begin
loadjs 2 finished
file 2 callback,NOT IE

CHROME:
write begin
write finished
outer js callback, not IE
loadjs 1 begin
loadjs 1 finished
file 1 callback,NOT IE
loadjs 2 begin
loadjs 2 finished
file 2 callback,NOT IE

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wddjpf.html