我用下面的加載腳本接二連三(async=false
):
var loadScript = function(scriptUrl, afterCallback) {
var firstScriptElement = document.getElementsByTagName('script')[0];
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.async = false;
scriptElement.src = scriptUrl;
var ieLoadBugFix = function (scriptElement, callback) {
if (scriptElement.readyState == 'loaded' || scriptElement.readyState == 'complete') {
callback();
} else {
setTimeout(function() { ieLoadBugFix(scriptElement, callback); }, 100);
}
}
if (typeof afterCallback === "function") {
if (typeof scriptElement.addEventListener !== "undefined") {
scriptElement.addEventListener("load", afterCallback, false)
} else {
scriptElement.onreadystatechange = function(){
scriptElement.onreadystatechange = null;
ieLoadBugFix(scriptElement, afterCallback);
}
}
}
firstScriptElement.parentNode.insertBefore(scriptElement, firstScriptElement);
}
使用方法如下:
loadScript('url/to/the/first/script.js', function() {
loadScript('url/to/the/second/script.js', function() {
// after both scripts are loaded
});
});
一個bug修正該腳本包括對IE的延遲錯誤。
至極版本你用? – reporter
你對IE有什麼問題?這是一條錯誤消息嗎?需要更多細節。 –
IE 7並沒有顯示錯誤,但沒有加載功能! – Marco