Code snippets

From GreaseSpot Wiki
Revision as of 19:21, 15 March 2007 by Arantius (talk | contribs)
Jump to navigationJump to search

xpath helper

function xpath(p, context) {
    if (!context) context=document;
    var arr=[];
    var xpr=document.evaluate(
        p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
    );
    for(i=0;item=xpr.snapshotItem(i);i++){ arr.push(item); }
    return arr;
}

Run a particular xpath expression p against the context node context (or the document, if not provided). Return the results as an array.