DOMParser().parseFromString()没有用Firefox给出响应 [英] DOMParser().parseFromString() not giving response with Firefox

查看:321
本文介绍了DOMParser().parseFromString()没有用Firefox给出响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个chrome扩展程序,一切正常. 现在我需要把它放到Firefox上,这真是一团糟.

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

问题出在dom解析上.

The problem is with dom parsing.

这是在FF上不起作用的代码:

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

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

SOURCE_DOM总是返回空对象:

SOURCE_DOM always return an object empty :

Object : {location : null}

在chrome上没有问题,它为我提供了文档对象,我可以正常使用它.但是,与chrome相比,Firefox在扩展扩展方面麻烦很多.

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 ?

推荐答案

使用下面的代码

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

这篇关于DOMParser().parseFromString()没有用Firefox给出响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆