Over a million developers have joined DZone.

XPath Selection

·
//test post

//Select elements based on an xpath expression.  Modified from the sample provided at greasemonkey's wiki.  Replaced array copy of elements with a closure to return snapshot item.

//p = xpath expression
//context = context the expression is evaluated on. e.g., document.
//returns snapshot of the xpath evaluation (i.e., elements, possibly attributes).


	function $XPathSelect(p, context) 
	{
	  if (!context) context = document;
	  var xpr = document.evaluate(p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	  return function(x) { return xpr.snapshotItem(x); };
	}
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}