我看到IE 11中不支持XPathEvaluator,但是我想知道是否有适当的检测机制来检查它是否存在,如果不返回到IE中的selectSingleNode方法。
不过,与此类似的是,每当我以这种方式检查XPathEvaluator时,它就会在IE11中爆炸,但在Firefox/Chrome中工作。
if (XPathEvaluator) {
var xpe = new XPathEvaluator();
...... evaluation logic
return results.singleNodeValue;
}
else {
return xmlDoc.selectSingleNode(elPath);
}以前的逻辑过去依赖于window.ActiveXObject的存在来调用selectSingleNode,但是该属性在IE11中已经被删除,从而导致XPathEvaluator逻辑被击中。
我宁愿检测这个功能是否存在,而不检查浏览器版本,因为功能和功能在不断变化。
这是我的简单测试用例。
IE 11会提醒I不是IE弹出,然后在XPath上爆炸。
FF/Chrome会提醒I不是IE弹出,然后提醒XPathEvaluator是一次尝试。
function selectSingleNode()
{
// previous logic relied on this to call XPathEvaluator
if(window.ActiveXObject)
{
alert('Im IE');
}
else
{
alert('I am Not IE');
}
// I wanted to do something like this.
if(XPathEvaluator)
{
alert('XPathEvaluator is a go');
}
else
{
alert('XPathEvaluator is a no go');
}
}发布于 2014-11-13 16:36:29
如果您想使用某种方法,那么检查它,所以如果您想使用selectSingleNode,那么请执行以下操作
if (typeof xmlDoc.selectSingleNode != 'undefined') {
// now use selectSingleNode method here
}我不知道为什么要检查XPathEvaluator,如果要检查文档节点上是否有evaluate方法来使用W3C DOM级别3 XPath API
if (typeof xmlDoc.evaluate != 'undefined') {
// now use evaluate method here
}所以你可以一起检查
if (typeof xmlDoc.evaluate != 'undefined') {
// now use evaluate method here
}
else if (typeof xmlDoc.selectSingleNode != 'undefined') {
// now use selectSingleNode method here
}https://stackoverflow.com/questions/26913265
复制相似问题