您的位置:首页 > Web前端 > Node.js

Firefox模拟selectNodes IE10IE11

2014-11-07 19:31 591 查看
IE10 IE11不支持Node.selectNodes() 和 Node.selectSingleNode() 也不支持 Document.evaluate() ??

node.selectSingleNode--报selectSingleNode is undefined
node.selectNodes----报selectSingleNode is undefined

自己用DOM Level 3扩展同样报错
_selectSingleNode: function (node, xpathExpr) {
if (typeof (node.selectSingleNode) != "undefined") {
return node.selectSingleNode(xpathExpr);
}
else {
//XPathEvaluator报未定义
//XPathResult报未定义
var xpe = new XPathEvaluator();
var xPathNode = xpe.evaluate(xpathExpr, node, MyLibrary._NSResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return (xPathNode != null) ? xPathNode.singleNodeValue : null;
}
}

function _selectNodes(node, XPathExpression) {
if (typeof (node.selectNodes) != "undefined") {
return node.selectNodes(XPathExpression);
}
else {
var output = [];
//evaluate报未定义
//XPathResult报未定义
var XPathResults = node.evaluate(XPathExpression, node, _NSResolver, XPathResult.ANY_TYPE, null);
var result = XPathResults.iterateNext();
while (result) {
output.push(result);
result = XPathResults.iterateNext();
}
return output;
}
}IE 10 does not support "selectSingleNode" (IE 7, 8, 9) and at the same time it does not support "XPathEvaluator"

/**------------------------------------------------------------------------------------------------------------------------------*/

Firefox模拟selectNodes:

var isIe=false;

var isFf=false;

var FFrv='';

if (navigator.appName.indexOf("Microsoft") != -1){
isIe=true;

} else if (navigator.appName  == 'Netscape' && navigator.userAgent.indexOf("Trident") != -1){
isIe=11;

} else {
isFf=true;
FFrv = parseFloat(navigator.userAgent.split("rv:")[1])

}

// 添加方法,兼容FireFox、Chrome。

if (!isIe)

{

    XMLDocument.prototype.selectSingleNode = Element.prototype.selectSingleNode = function (xpath)

    {

        var  x = this.selectNodes(xpath)

        if ( ! x || x.length < 1 ) return   null ;

        return  x[ 0 ];

    }

    XMLDocument.prototype.selectNodes = Element.prototype.selectNodes = function (xpath)

    {

        var  xpe  =   new  XPathEvaluator();

        var  nsResolver  =  xpe.createNSResolver( this.ownerDocument  ==   null?this.documentElement :  this.ownerDocument.documentElement);

        var  result  =  xpe.evaluate(xpath,  this , nsResolver,  0 ,  null );

        var  found  =  [];

        var  res;

        while  (res  =  result.iterateNext())

            found.push(res);

        return  found;

    }
}

/***----------------------------------------------------------------------------------------------------------------------------------------------------*/

IE10和IE11解决方法:下面网址

http://www.iefans.net/ie10-xmlhttprequest-responsexml/

Windows 8 Release
Preview 中的 IE10 更新 XMLHttpRequest 的 responseXML,XMLHttpRequest 以便默认返回本机 XML 文档。这一更改适用于 IE10 的标准和 Quirks 文档模式,从而使其能够与其他当代浏览器互操作,并与“相同标记”方法保持一致。兼容性文档模式 5、7、8 和 9 保持不变。

该更改可能影响期望responseXML responseXML 包含 MSXML 文档且依赖于 MSXML 特定功能(如 selectNodes)的站点。selectNodes.在这些情况下,您可通过将 XMLHttpRequest 对象的 responseType 成员设置为“msxml-document”,responseType 来请求 IE10 返回XMLHttpRequest MSXML 文档。'msxml-document'.如果您的代码不依赖于 MSXML 特定功能,则 IE10 的本机 XML
文档可以有效地供您使用。

IE9 中的本机 XML 支持带来了类似于 XML 和 HTML 的 DOM,并支持直接在页面中(甚至在 HTML 中)插入和呈现 XML 片段。通过新增了 DOMParser
和 XMLSerializer,IE9 还简化了 XML 与 DOM 之间的转换。IE10 通过更新 responseXML 以返回本机 XML 文档responseXML 来完成该转换。

与 IE9 类似,IE10 会在 Windows 8 Release Preview 针对 responseXML 返回 MSXML 文档前进行预览。responseXML.结果,检索本机文档需要一个附加步骤,即将
responseText 传递给responseText DOMParser。DOMParser.

var xhr = new XMLHttpRequest();
 
//...
 
var parser = new DOMParser();
 
var doc = parser.parseFromString(xhr.responseText, 'text/xml');
 
// 'doc' contains a native document in both IE9 and IE10


在 Windows 8 Release Preview 中,由于直接通过 responseXML 返回本机文档,DOMParser 因此 IE10 无需这个附加步骤。responseXML.使用 DOMParser 的现有代码DOMParser 可以继续在 IE10 中照常工作。

var xhr = new XMLHttpRequest();
 
//...
 
var doc = xhr.responseXML;
 
// 'doc' contains a native document in IE10’s Standards and Quirks document modes
 
// it contains an MSHTML document in IE9 and in IE10’s compatibility document modes


当 responseType 设置为“document”时,response 这种简便性也适用于responseType 新 response 属性。'document'.

var xhr = new XMLHttpRequest();
 
xhr.open(method, url, true);
 
xhr.responseType = 'document';
 
//...
 
var doc = xhr.response;
 
// 'doc' contains a native document in IE10’s Standards and Quirks document modes


IE10 还另外包含一种用来检索 MSXML 文档的机制。如果您仍然需要一些 MSXML 特定功能(如 selectNodes),或者只是需要一些额外的时间来进行迁移, selectNodes那么该机制是比较有用的。为此,请将您 XMLHttpRequest 对象的responseType responseType 设置为XMLHttpRequest “msxml-document”。'msxml-document'.

var xhr = new XMLHttpRequest();
 
xhr.open(method, url, true);
 
try { xhr.responseType = 'msxml-document'; } catch(e){}
 
//...
 
var doc = xhr.responseXML;
 
// 'doc' now contains an MSXML document in IE10’s Standards and Quirks document modes


理论上讲,该分配应被其他浏览器忽略,但实际上,一些浏览器会引发异常。您可以利用 try/catch 语句防止这一弊端,try/catch 如上面的示例所示。

—Internet Explorer 项目经理 Tony Ross

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明

文章引用地址:http://www.iefans.net/ie10-xmlhttprequest-responsexml/ 作者:iefans
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  firefox selectNodes IE11