IE 7&8不会加载脚本

IE 7 & 8 wont load script

本文关键字:加载 脚本 amp IE      更新时间:2023-09-26

我正在使用以下代码为我的小部件加载所需的JS库:

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中都能很好地工作。

但在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

有什么办法解决这个问题吗?我的头撞到墙上这个问题

完全跨浏览器测试的脚本:

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();
            }
        })
    }
});