Skip to content

Instantly share code, notes, and snippets.

@fearphage
Created October 30, 2009 12:09
Show Gist options
  • Save fearphage/222294 to your computer and use it in GitHub Desktop.
Save fearphage/222294 to your computer and use it in GitHub Desktop.
IE-inspired convenience wrappers for document.evaluate
if ((typeof Node != 'undefined') && !document.selectNodes) {
Node.prototype.selectNodes = function(xpath, resolver) {
var contextNode = this.ownerDocument || this, result = [], i = 0, node
,nodes = contextNode.evaluate(xpath, contextNode, resolver || null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
while (node = nodes.snapshotItem(i++)) {
result.push(node);
}
return result;
};
Node.prototype.selectSingleNode = function(xpath, resolver) {
return document.evaluate(xpath, this.ownerDocument || this, resolver || null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment