javascript加载xml,在chrome下失败的解决办法

原来的写法:

this.loadxml = function(xml) {
    var xmlDoc;
    if (window.ActiveXObject) {
        xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
        xmlDoc.async = false;
        xmlDoc.load(xml);
    } else if (document.implementation && document.implementation.createDocument) {
        xmlDoc = document.implementation.createDocument('', '', null);
        xmlDoc.async = false;
        xmlDoc.load(xml);
    } else {
        return null;
    }

    return xmlDoc;
};

修改后的写法:

this.loadxml = function(xml) {
    var xmlDoc;
    if (window.ActiveXObject) {
        xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
        xmlDoc.async = false;
        xmlDoc.load(xml);
    } else if (document.implementation && document.implementation.createDocument) {
        try{
            xmlDoc = document.implementation.createDocument('', '', null);
            xmlDoc.async = false;
            xmlDoc.load(xml);
        } catch(e){
            var xmlhttp = new window.XMLHttpRequest();
            xmlhttp.open("GET",xml,false);
            xmlhttp.send(null);
            xmlDoc = xmlhttp.responseXML.documentElement;
        }
    } else {
        return null;
    }

    return xmlDoc;
};

相关推荐