Code snippets: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
No edit summary
 
No edit summary
Line 4: Line 4:
     if (!context) context=document;
     if (!context) context=document;
     var arr=[];
     var arr=[];
     var xpr=document.evaluate(p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
     var xpr=document.evaluate(
        p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
    );
     for(i=0;item=xpr.snapshotItem(i);i++){ arr.push(item); }
     for(i=0;item=xpr.snapshotItem(i);i++){ arr.push(item); }
     return arr;
     return arr;

Revision as of 19:21, 15 March 2007

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.