0

我有一大堆javascript文件需要按顺序加载。但是,其中一个不在ie7中加载。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 
+1

http://blog.andrewcantino.com/blog/2008/11/23/replacement-for-script-onload-in-ie/ – McGarnagle

+0

onloads会触发,而不是console.log()对于'a '和'b',它们被捆绑在callbacka –

回答

0

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

+0

捆绑中肯定是可取的,但OP没有提到他正在使用IIS或4.5(尚未发布)。 –

+0

我知道。我只想表明我将如何解决这个问题。 –