DOMParser().parseFromString() not giving response with Firefox

隐身守侯 提交于 2019-12-24 16:37:24

问题


I built a chrome extension and everything worked well. Now i need to put it on firefox, and it's a f*** mess.

The problem is with dom parsing.

Her's the code that doesn't work on FF :

var parser = new DOMParser();
SOURCE_DOM = parser.parseFromString(data.url, "text/html");

SOURCE_DOM always return an object empty :

Object : {location : null}

On chrome there's no problem with that, it gives me the document object and i can properly work with it. But Firefox is a pain in the ass compared to chrome when it comes to extension building.

Someone would know how to get the document ?


回答1:


Use the code below

if (window.DOMParser) {
    var parser=new window.DOMParser();
    var parsererrorNS = null;
    // IE9+ now is here
    if(!isIEParser) {
        try {
            parsererrorNS = parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0].namespaceURI;
        }
        catch(err) {
            parsererrorNS = null;
        }
    }
    try {
        xmlDoc = parser.parseFromString( xmlDocStr, "text/xml" );
        if( parsererrorNS!= null && xmlDoc.getElementsByTagNameNS(parsererrorNS, "parsererror").length > 0) {
            //throw new Error('Error parsing XML: '+xmlDocStr);
            xmlDoc = null;
        }
    }
    catch(err) {
        xmlDoc = null;
    }
} else {
    // IE :(
    if(xmlDocStr.indexOf("<?")==0) {
        xmlDocStr = xmlDocStr.substr( xmlDocStr.indexOf("?>") + 2 );
    }
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(xmlDocStr);
}


来源:https://stackoverflow.com/questions/32841067/domparser-parsefromstring-not-giving-response-with-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!