【发布时间】:2012-06-25 21:35:00
【问题描述】:
我有一大堆需要按顺序加载的 javascript 文件。但是,其中一个没有在 ie7 中加载。
这是执行加载的函数:
function loadScript(url, callback){
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = url;
// Attach handlers for all browsers
var done = false;
script.onload = script.onreadystatechange = function()
{
if( !done && ( !this.readyState
|| this.readyState == "loaded"
|| this.readyState == "complete") )
{
done = true;
// Continue your code
callback();
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
head.removeChild( script );
}
};
head.appendChild(script);
}
函数调用:
loadScript('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js',function(){
loadScript('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js',function(){
loadScript('http://XXX/js/data.php?rand='+Math.random(),function(){
loadScript('http://XXX/js/jquery.inject.js?rand='+Math.random(),function(){
console.log('a');
loadScript('XXX/js/press.js?rand='+Math.random(),function(){
console.log('b');
inject_press();
});
});
});
});
});
jquery.inject.js 没有加载的文件,谁的代码是
console.log('y');
jQuery.prototype.inject = function(a){
...
}
这同样适用于所有浏览器除了 ie7。输出是
a
b
【问题讨论】:
-
onloads 会触发,而不是 'a' 和 'b' 的 console.log()s,它们隐藏在回调中a
标签: javascript jquery internet-explorer internet-explorer-7 cross-browser