当前位置: 移动技术网 > IT编程>开发语言>Java > 浅谈js文件引用方式及其同步执行与异步执行

浅谈js文件引用方式及其同步执行与异步执行

2019年07月22日  | 移动技术网IT编程  | 我要评论

技师学院毕业证,盘阳机场,excel图表

任何以appendchild(scriptnode) 的方式引入的js文件都是异步执行的 (scriptnode 需要插入document中,只创建节点和设置 src 是不会加载 js 文件的,这跟 img 的与加载不同 )

html文件中的<script>标签中的代码或src引用的js文件中的代码是同步加载和执行的

html文件中的<script>标签中的代码使用document.write()方式引入的js文件是异步执行的

html文件中的<script>标签src属性所引用的js文件的代码内再使用document.write()方式引入的js文件是同步执行的

1、

<script>
//同步加载执行的代码
</script>

2、

<script src="xx.js"></script> //同步加载执行xx.js中的代码

3、

<script>
document.write('<script src="xx.js"><\/script>');  //异步加载执行xx.js中的代码
</script>

4、

<script src="xx.js"></script>

xx.js中有下面代码:

document.write('<script src="11.js"><\/script>');  
document.write('<script src="22.js"><\/script>');  

则xx.js和11.js、22.js 都是同步加载和执行的。

如果 xx.js 以插入方式异步加载,则 11.js 和 22.js 仍然是同步加载的(异步中的同步,即,这2个文件的加载是分先后次序的)

测试:在11中 alert, 22中 document.write() ,可以看到 22中写入语句被阻塞

5、

下面这种方式,xx.js会在appendchild执行之后异步加载执行

var script = document.createelement("script");
script.setattribute("src","xx.js");
documenrt.getelementsbytagname("head")[0].appendchild(script);

一个加载 js 文件的 函数:

var loadjs = function(url,callback){ 
var head = document.getelementsbytagname('head')[0],  
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标准的
if ((!this.readystate) || this.readystate == "complete" || this.readystate == "loaded" ){ 
 callback();   
 }
 else
 {     
 alert("can not load the js file")   
 }  
 }
 }

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

tryjs.html

<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<script src="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="try.1.js" onreadystatechange="console.log(\'file 1 callback \',this.readystate,\' ie\');" onload="if(!document.all){console.log(\'file 1 callback,not ie \');}"><\/script>');
document.write('<script src="try.2.js" onreadystatechange="console.log(\'file 2 callback \',this.readystate,\' ie\');" onload="if(!document.all){console.log(\'file 2 callback,not 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

以上就是小编为大家带来的浅谈js文件引用方式及其同步执行与异步执行的全部内容了,希望对大家有所帮助,多多支持移动技术网~

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网