淺談js檔案引用方式及其同步執行與非同步執行_java

來源:互聯網
上載者:User

任何以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檔案引用方式及其同步執行與非同步執行的全部內容了,希望對大家有所協助,多多支援雲棲社區~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.