我尝试了https://stackoverflow.com/a/950146/151453提供的代码,并成功验证我可以t2.js
从t1.js
. 但是,t2.js
完成加载回调仅适用于 Chrome(v26)、Firefox(v17)和 IE10,不适用于 Microsoft IE8(Windows 7)。
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 上,如果我启用script.onreadystatechange = callback;
in ,则会loadScript()
弹出警告框,但是,调用loadScript('t3.js.');
失败并在该行显示“未实现错误”,因此 t3.js 无法加载。下图: