js文件同步加載
須先加載的js文件的尾部,加上如下相關(guān)代碼,可以達(dá)到同步加載多個(gè)js文件的效果:
var oHead = document.getElementsByTagName('body').item(0);
var oScript = document.createElement("script");
oScript.type = "text/javascript";
oScript.src = "../middleService/deptEnter/js/UI_index.js";
oHead.appendChild(oScript
HTML 4.01 的script屬性
charset: 可選。指定src引入代碼的字符集,大多數(shù)瀏覽器忽略該值。
defer: boolean, 可選。延遲腳本執(zhí)行,相當(dāng)于將script標(biāo)簽放入頁(yè)面body標(biāo)簽的底部,js腳本會(huì)在document的DOMContentLoaded之前執(zhí)行。除IE和較新版本的Firefox外,其他瀏覽器并未支持。
language: 已廢棄。大部分瀏覽器會(huì)忽略該值。
src: 可選。指定引入的外部代碼文件,不限制后綴名。
type: 必選。指定腳本的內(nèi)容類型(MIME類型),F(xiàn)實(shí)中通常不指定該值也可以,瀏覽器會(huì)默認(rèn)當(dāng)作text/javascript類型來(lái)解釋執(zhí)行。
HTML5中的script屬性:
script 標(biāo)簽在HTML5中除了具備HTML5新標(biāo)準(zhǔn)定義的屬性以外,和HTML4.01相比移除了language屬性,修改了type屬性為可選的(默認(rèn)text/javascript),并新增了一個(gè)屬性async。
async boolean, 屬性的作用,定義腳本是否異步執(zhí)行,取值true或false。
如果 async 設(shè)為 true ,會(huì)忽略 defer 屬性。
設(shè)置為異步執(zhí)行的 js 文件被假定為不使用 document.write() 想加載中的 document 寫入內(nèi)容,因此不要在 異步執(zhí)行的 js 文件的加載執(zhí)行 過(guò)程中使用 document.write()
除了 script 標(biāo)簽屬性外,頁(yè)面引入 js 文件的方式影響其加載執(zhí)行方式:
任何以appendChild(scriptNode) 的方式引入的js文件都是異步執(zhí)行的 (scriptNode 需要插入document中,只創(chuàng)建節(jié)點(diǎn)和設(shè)置 src 是不會(huì)加載 js 文件的,這跟 img 的預(yù)加載不能類比 )
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í)行的
不要使用類似下面這種做法,這樣并不會(huì)發(fā)起加載 js 文件的請(qǐng)求:
divNode.innerHTML = '<script src="xxx.js"></script>';
=====================================================
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個(gè)文件的加載是分先后次序的)
測(cè)試:在11中 alert, 22中 document.write() ,可以看到 22中寫入語(yǔ)句被阻塞
5、
下面這種方式,xx.js會(huì)在appendChild執(zhí)行之后異步加載執(zhí)行
var script = document.createElement("script");
script.setAttribute("src","xx.js");
documenrt.getElementsByTagName("head")[0].appendChild(script);
一個(gè)加載 js 文件的 函數(shù):
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 標(biāo)簽,IE 下有 onreadystatechange 事件, w3c 標(biāo)準(zhǔn)有 onload 事件
//這些 readyState 是針對(duì)IE8及以下的,W3C 標(biāo)準(zhǔn)因?yàn)閟cript 標(biāo)簽沒(méi)有這個(gè) onreadystatechange 所以也不會(huì)有 this.readyState , 文件加載不成功 onload 不會(huì)執(zhí)行,
//(!this.readyState) 是針對(duì) W3C標(biāo)準(zhǔn)的, IE 9 也支持 W3C標(biāo)準(zhǔn)的 onload
if ((!this.readyState) || this.readyState == "complete" || this.readyState == "loaded" ){
callback();
}else{
alert("can not load the js file");
}
}//end onreadystatechange
}
對(duì)于第4點(diǎn)的測(cè)試(其中插入 alert 很容易看到加載時(shí)的阻塞)
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');
測(cè)試結(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