【问题标题】:Javascript resource not loading in ie7Javascript资源未在ie7中加载
【发布时间】: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

【问题讨论】:

标签: javascript jquery internet-explorer internet-explorer-7 cross-browser


【解决方案1】:

这不是加载 ECMAscript 文件的最佳方式。我会命名这些文件以对其进行排序,然后加载using ASP.NET 4.5 bundling

【讨论】:

  • 捆绑绝对是可取的,但 OP 没有提到他正在使用 IIS 或 4.5(尚未发布)。
猜你喜欢
  • 2020-10-15
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
相关资源
最近更新 更多