IE8中動(dòng)態(tài)創(chuàng)建script標(biāo)簽onload無效的解決方法
本文實(shí)例講述了IE8中動(dòng)態(tài)創(chuàng)建script標(biāo)簽onload無效的解決方法。分享給大家供大家參考。具體分析如下:
今天做項(xiàng)目,發(fā)現(xiàn)一個(gè)奇怪的問題,動(dòng)態(tài)創(chuàng)建的script標(biāo)簽在IE8下無法觸發(fā)onload事件。
代碼如下:
var script = null;
script = document.createElement("script");
script.type = "text/javascript";
script.src = src;
if(typeof fun === "function"){
script.onload = fun;
}
document.getElementsByTagName("head")[0].appendChild(script);
};
loadJs("js/jquery-1.11.0.min.js", function(){
console.log("From jQuery");
});
loadJs("test.js", function(){
console.log("From test.js");
});
test.js:
console.log(typeof jQuery);
運(yùn)行結(jié)果:
>> typeof jQuery // 從控制臺(tái)上運(yùn)行,卻找到了jQuery對象,證明加載順序問題
"function"
并且以上代碼中script.onload并沒有執(zhí)行,明明代碼已經(jīng)加載進(jìn)來了,為什么還是onload不執(zhí)行呢?到網(wǎng)上一查發(fā)現(xiàn)眾多前端開發(fā)人員都遇到這個(gè)棘手的問題,于是找到了一些替補(bǔ)方案,如下:
var script = null;
script = document.createElement("script");
script.type = "text/javascript";
script.src = src;
if(typeof fun === "function"){
script.onreadystatechange = function() {
var r = script.readyState;
console.log(src + ": " + r);
if (r === 'loaded' || r === 'complete') {
script.onreadystatechange = null;
fun();
}
};
}
document.getElementsByTagName("head")[0].appendChild(script);
};
執(zhí)行結(jié)果:
js/jquery-1.11.0.min.js: loading
test.js: complete
From test.js
js/jquery-1.11.0.min.js: loaded
From jQuery
執(zhí)行步驟為,這下類似于onload的功能算然算是找到了,但卻有一個(gè)問題,它不是按順序加載的,當(dāng)jQuery文件loading的時(shí)候,test.js已經(jīng)complete了,并且第一行就先執(zhí)行了test.js的內(nèi)容。因?yàn)閠est.js先于jQuery執(zhí)行,所以才打出undefined。于是我們可以改寫成這樣,讓它線性加載:
console.log("From jQuery");
loadJs("test.js", function(){
console.log("From test.js");
});
});
執(zhí)行結(jié)果:
js/jquery-1.11.0.min.js: loaded
From jQuery
function
test.js: complete
From test.js
這次,執(zhí)行的順序完全是按照我們預(yù)訂的順序來了,但以上代碼看著很別扭,需要層層嵌套,于是又發(fā)現(xiàn)了這種寫法:
var script = null;
script = document.createElement("script");
script.type = "text/javascript";
script.src = src;
if(typeof fun === "function"){
script.onreadystatechange = function() {
var r = script.readyState;
console.log(src + ": " + r);
if (r === 'loaded' || r === 'complete') {
script.onreadystatechange = null;
fun();
}
};
}
document.write(script.outerHTML);
//document.getElementsByTagName("head")[0].appendChild(script);
};
loadJs("js/jquery-1.11.0.min.js", function(){
console.log("From jQuery");
});
loadJs("test.js", function(){
console.log("From test.js");
});
執(zhí)行結(jié)果的順序,也不相同:
js/jquery-1.11.0.min.js: loaded
From jQuery
test.js: loaded
From test.js
如果你改變一下加載順序
console.log("From test.js");
});
loadJs("js/jquery-1.11.0.min.js", function(){
console.log("From jQuery");
});
執(zhí)行結(jié)果也就不一樣,類似順序加載:
test.js: loaded
From test.js
js/jquery-1.11.0.min.js: loaded
From jQuery
希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。
- JavaScript動(dòng)態(tài)添加css樣式和script標(biāo)簽
- 動(dòng)態(tài)創(chuàng)建script標(biāo)簽實(shí)現(xiàn)跨域資源訪問的方法介紹
- Script標(biāo)簽與訪問HTML頁面詳解
- javascript標(biāo)簽在頁面中的位置探討
- script標(biāo)簽屬性type與language使用選擇
- script標(biāo)簽的 charset 屬性使用說明
- javascript 獲取url參數(shù)和script標(biāo)簽中獲取url參數(shù)函數(shù)代碼
- asp.net(C#) 動(dòng)態(tài)添加非ASP的標(biāo)準(zhǔn)html控件(如添加Script標(biāo)簽)
- 有趣的script標(biāo)簽用getAttribute方法來自腳本吧
- 淺談js script標(biāo)簽中的預(yù)解析
相關(guān)文章
JavaScript實(shí)現(xiàn)的文本框placeholder提示文字功能示例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的文本框placeholder提示文字功能,涉及javascript事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-07-07JS實(shí)現(xiàn)鍵值對遍歷json數(shù)組功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)鍵值對遍歷json數(shù)組功能,結(jié)合實(shí)例形式分析了javascript遍歷json數(shù)組相關(guān)操作技巧,需要的朋友可以參考下2018-05-05靜態(tài)的動(dòng)態(tài)續(xù)篇之來點(diǎn)XML
靜態(tài)的動(dòng)態(tài)續(xù)篇之來點(diǎn)XML...2006-08-08JS實(shí)現(xiàn)聯(lián)想、自動(dòng)補(bǔ)齊國家或地區(qū)名稱的功能
這篇文章主要介紹了JS實(shí)現(xiàn)聯(lián)想、自動(dòng)補(bǔ)齊國家或地區(qū)名稱的功能,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07JavaScript轉(zhuǎn)換二進(jìn)制編碼為ASCII碼的方法
這篇文章主要介紹了JavaScript轉(zhuǎn)換二進(jìn)制編碼為ASCII碼的方法,涉及javascript編碼轉(zhuǎn)換的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04