在ie7中没有加载Javascript资源

时间:2012-06-24 07:10:35

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

我需要按顺序加载一大堆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();
                });

            });
        });
    });
});

没有加载i jquery.inject.js的文件,代码是

console.log('y');

jQuery.prototype.inject = function(a){
    ...
}

此外,这适用于所有浏览器 ie7除外。输出是

a
b

1 个答案:

答案 0 :(得分:0)

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