2013-02-10 66 views
0

我使用下面的代碼在需要的JS庫加載控件我:IE 7和8不會加載腳本

function loadScrip(url, callback) { 
    var script = document.createElement("script") 
    script.type = "text/javascript"; 

    if (script.readyState) { //IE 
     script.onreadystatechange = function() { 
      if (script.readyState == "loaded" || script.readyState == "complete") { 
       script.onreadystatechange = null; 
       callback(); 
      } 
     }; 
    } else { //Others 
     script.onload = function() { 
      callback(); 
     }; 
    } 
    script.src = url; 
    document.getElementsByTagName("head")[0].appendChild(script); 
} 


loadScrip("http://code.jquery.com/jquery-1.8.3.js", function() { 
    loadScrip("http://code.jquery.com/ui/1.9.2/jquery-ui.js", function() { 
     alert("Loaded"); 
     js13 = jQuery.noConflict(true); 
     main(); 
    }); 
}); 

這適用於所有的瀏覽器和IE9 absolutley罰款。

但在IE 7 & 8出於某種原因,jQuery的ui.js不會加載和我得到的錯誤:

Line: 563 
Character: 4 
Code: 0 
Error Message: Object doesn't support this property or method 
URL: http://code.jquery.com/jquery-1.8.3.js 

任何想法如何解決這一問題?林撞我的頭靠在牆上這個問題

+1

你爲什麼要同時裝入的jQuery 1.8.3和1.9.2 ? – DevelopmentIsMyPassion 2013-02-10 16:50:11

+0

如果您使用純腳本標記jquery-ui.js被加載並且工作? – rene 2013-02-10 16:51:55

+1

@AshReva沒有那是jQuery 1.8.3和jQuery ** UI ** 1.9.2 – Antony 2013-02-10 17:06:42

回答

0

設施的跨瀏覽器測試腳本:

var CFLoad = { 
    fScript : null, 
    isFileReady : function (v) { 
     return (! v || v == "loaded" || v == "complete" || v == "uninitialized"); 
    }, 
    js : function(src,cb,attrs) { 
     var s = document.createElement("script"), 
      done = !1, i; 
     s.src = src; 
     s.type = "text/javascript"; 
     for (i in attrs) { 
      s.setAttribute(i, attrs[ i ]); 
     } 
     s.onreadystatechange = s.onload = function() { 
      if (! done && CFLoad.isFileReady(s.readyState)) { 
       done = !0; 
       if(cb) cb(s); 
       s.onload = s.onreadystatechange = null; 
      } 
     }; 
     window.setTimeout(function() { 
      if(!done) { 
       done = !0; 
       if(cb) cb(s,1); 
      } 
     }, 5000); 
     if(this.fScript===null) this.init(); 
     this.fScript.parentNode.insertBefore(s, this.fScript); 
    }, 
    css : function(href,cb,attrs) { 
     var l = document.createElement("link"),i; 
     l.href = href; 
     l.rel = "stylesheet"; 
     l.type = "text/css"; 
     for (i in attrs) { 
      l.setAttribute(i, attrs[i]); 
     } 
     if(this.fScript===null) this.init(); 
     this.fScript.parentNode.insertBefore(l,this.fScript); 
     if(cb) window.setTimeout(cb, 0); 
    }, 
    init : function() { 
     this.fScript = document.getElementsByTagName("script")[ 0 ]; 
    } 
}; 

使用

CFLoad.js("http://code.jquery.com/jquery-1.8.3.js", function (script_tag, failed) { 
    if(!failed) { 
     CFLoad.js("http://code.jquery.com/ui/1.9.2/jquery-ui.js", function(s, f) { 
      if(!f) { 
       alert("Loaded"); 
       js13 = jQuery.noConflict(true); 
       main(); 
      } 
     }) 
    } 
}); 
+0

哇,這太棒了!感謝一羣分享此! – Laine 2013-02-10 17:34:58