淺談js文件引用方式及其同步執(zhí)行與異步執(zhí)行
任何以appendChild(scriptNode) 的方式引入的js文件都是異步執(zhí)行的 (scriptNode 需要插入document中,只創(chuàng)建節(jié)點(diǎn)和設(shè)置 src 是不會加載 js 文件的,這跟 img 的與加載不同 )
html文件中的<script>標(biāo)簽中的代碼或src引用的js文件中的代碼是同步加載和執(zhí)行的
html文件中的<script>標(biāo)簽中的代碼使用document.write()方式引入的js文件是異步執(zhí)行的
html文件中的<script>標(biāo)簽src屬性所引用的js文件的代碼內(nèi)再使用document.write()方式引入的js文件是同步執(zhí)行的
1、
<script> //同步加載執(zhí)行的代碼 </script>
2、
<script src="xx.js"></script> //同步加載執(zhí)行xx.js中的代碼
3、
<script> document.write('<script src="xx.js"><\/script>'); //異步加載執(zhí)行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 都是同步加載和執(zhí)行的。
如果 xx.js 以插入方式異步加載,則 11.js 和 22.js 仍然是同步加載的(異步中的同步,即,這2個文件的加載是分先后次序的)
測試:在11中 alert, 22中 document.write() ,可以看到 22中寫入語句被阻塞
5、
下面這種方式,xx.js會在appendChild執(zhí)行之后異步加載執(zhí)行
var script = document.createElement("script"); script.setAttribute("src","xx.js"); documenrt.getElementsByTagName("head")[0].appendChild(script);
一個加載 js 文件的 函數(shù):
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 標(biāo)簽,IE 下有 onreadystatechange 事件, w3c 標(biāo)準(zhǔn)有 onload 事件 //這些 readyState 是針對IE8及以下的,W3C 標(biāo)準(zhǔn)因?yàn)閟cript 標(biāo)簽沒有這個 onreadystatechange 所以也不會有 this.readyState , // 好在文件加載不成功 onload 不會執(zhí)行,(!this.readyState) 是針對 W3C標(biāo)準(zhǔn)的 if ((!this.readyState) || this.readyState == "complete" || this.readyState == "loaded" ){ callback(); } else { alert("can not load the js file") } } }
對于第4點(diǎn)的測試(其中插入 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');
測試結(jié)果(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
以上就是小編為大家?guī)淼臏\談js文件引用方式及其同步執(zhí)行與異步執(zhí)行的全部內(nèi)容了,希望對大家有所幫助,多多支持腳本之家~
相關(guān)文章
java阻塞隊(duì)列BlockingQueue詳細(xì)解讀
這篇文章主要介紹了java阻塞隊(duì)列BlockingQueue詳細(xì)解讀,在新增的Concurrent包中,BlockingQueue很好的解決了多線程中,如何高效安全“傳輸”數(shù)據(jù)的問題,通過這些高效并且線程安全的隊(duì)列類,為我們快速搭建高質(zhì)量的多線程程序帶來極大的便利,需要的朋友可以參考下2023-10-10java返回集合為null還是空集合及空集合的三種寫法小結(jié)
這篇文章主要介紹了java返回集合為null還是空集合及空集合的三種寫法小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11簡單實(shí)現(xiàn)Java版學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了簡單實(shí)現(xiàn)Java版學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06淺談Java中Collections.sort對List排序的兩種方法
本文介紹了Java中Collections.sort對List排序的兩種方法以及Comparable 與Comparator區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12詳談cxf和axis兩種框架下的webservice客戶端開發(fā)
這篇文章主要介紹了詳談cxf和axis兩種框架下的webservice客戶端開發(fā),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08