日期:2014-05-16  浏览次数:20357 次

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

?

1 楼 enemy_99 2011-12-27  
如果xml是个Document对象呢?