我嘗試在https://stackoverflow.com/a/950146/151453處提供的代碼,並且我成功驗證了我可以從t1.js
加載我的t2.js
。但是,t2.js
完成加載回叫僅適用於Chrome(v26),Firefox(v17)和IE10,,但不適用於Microsoft IE8(Windows 7)。如何使用IE8包含js js,使用IE8
IE8上的症狀是:從不調用回調start_deco()
。
我怎樣才能在IE8中實現相同的結果?謝謝。
====代碼如下====
t1.html:
<html>
<head></head>
<body>
Hello!
<script src="t1.js"></script>
</body>
</html>
t1.js:
// loadScript function provided by https://stackoverflow.com/a/950146/151453
function loadScript(url, callback)
{
// adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// then bind the event to the callback function
// there are several events for cross browser compatibility
//script.onreadystatechange = callback; // chj: !!!
script.onload = callback;
// fire the loading
head.appendChild(script);
}
function start_deco()
{
alert('Done t2.js.');
loadScript('t3.js.');
}
loadScript('t2.js', start_deco) // wish to load jquery.js etc
t2.js:
console.log('Loading t2.js...')
t3.js:
console.log('Loading t3.js...')
============== UPDATE1 =================
在IE8,如果我在loadScript()
中啓用script.onreadystatechange = callback;
,警告框會彈出,但是,調用loadScript('t3.js.');
失敗,並且該行上出現「未執行錯誤」,導致t3.js無法加載。如下圖:
您可能遇到安全問題,從IE8中的file:// URL運行。嘗試從Web服務器運行相同的代碼並查看它是否可用。 –
你在IE8中收到任何JavaScript錯誤嗎? (右下方是紅色的X) – wanovak
@MikeMcCaughan是對的 - 您是否安裝了IE開發工具或Firebug lite?看看控制檯,看看有什麼報告。 – CherryFlavourPez