Hey all I am looking to get the href link from some html using the Xpath like so:
function getStuff(Path) {
var txt = '';
var cnt = 0;
var nodes = document.evaluate(Path, document, null, XPathResult.ANY_TYPE, null);
var result = nodes.iterateNext();
var Json = '{testing:[';
while (result) {
if (cnt != 10) {
//console.log('Result: ', result);
txt += '{href:' + result + '},';
result = nodes.iterateNext();
cnt++;
} else {
txt = txt.replace(/(^,)|(,$)/g, );
break;
}
}
return Json + txt + ']}';
}
console.log(getStuff("//html/body/div[1]/div/div/div/div/div/div/div/descendant::a[contains(@href, '?fref=pb')]/@href"));
The output looks like this:
{testing:[{href:[object Attr]},{href:[object Attr]},{href:[object A...etc etc
However, if I un-comment the console.log it outputs like I need it to:
Result: href="/bob.barker.982?fref=pb"
Result: href="/bill.gates.982?fref=pb"
Result: href="/steve.jobs?fref=pb"
Result: href="/513845713656?fref=pb"
Result: href="/bill.murry?fref=pb"
Result: href="/m.c.hammer?fref=pb"
...etc etc
How can I get that same output that console.log has but saved to the txt+= logic? And if there is a better way of doing this then please do share.
Here is the JSfiddle Demo