JavaScript generate an result array from XPATH

This is a function in JavaScript that use an XPATH string to generate an Array from the results.This is very useful if you have to manipulate or use some items on a page and don’t have the id(like bots).

That is the function:

function matches2array (XPATH){
  var links = new Array();
  var elements = 0;
  var xPathRes = document.evaluate (XPATH, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
  var actualSpan = xPathRes.iterateNext ();
  while (actualSpan) {
    links[elements] = actualSpan;
    actualSpan = xPathRes.iterateNext ();
    ++elements;
  }
  return(links);
}

You could use it in the Terminal:

matches2array("//a");

And we get all hrefs on this page in the Terminal:

[a.skip-link.screen-reader-text #content, a scheinast.eu, a /category/c-sharp/, a /category/c/, a /category/fortran/, a /category/bash/, a /category...-css-js/, a /category/java/, a /category/perl/, a /category/php/, a /category/projects/, a /category/regex/, a /category...-tricks/, a /category/windows/, a /perl-str...nchmark/, a /regex-fi...strings/, a /how-to-o...desktop/, a /c-calculator/, a /javascri...ocation/, a /image-uploader/, a /wp-admin/, a wp-login....68d8990f, a /feed/, a /comments/feed/, a wordpress.org, a.a2a_button_facebook /, a.a2a_button_twitter /, a.a2a_button_google_plus /, a.a2a_dd.addtoany_share_save share_sav...2F%5D..., a /calculator-in-c/, a.url.fn.n /author/p...heinast/, a /category/c/, a /tag/c/, a /tag/calculator/, a.post-edit-link post.php?...ion=edit, a#cancel-comment-reply-link /calculat...#respond, a profile.php, a wp-login....68d8990f, a /new-hompage-design/, a /backtrack-how-to/, a.a2a_button_facebook /, a.a2a_button_twitter /, a.a2a_button_google_plus /, a.a2a_dd.addtoany_share_save share_sav...2F%5D..., a.screen-reader-shortcut #wp-toolbar, a.ab-item about.php, a.ab-item about.php, a.ab-item wordpress.org, a.ab-item codex.wordpress.org, a.ab-item /support/, a.ab-item requests-...feedback, a.ab-item admin.php...tics.php, a.ab-item admin.php...ine_menu, a.ab-item admin.php...tics.php, a.ab-item /wp-admin/, a.ab-item /wp-admin/, a.ab-item themes.php, a.ab-item customize...D=themes, a.ab-item customize...-in-c%2F, a.ab-item widgets.php, a.ab-item customize...=widgets, a.ab-item nav-menus.php, a.ab-item themes.ph...ckground, a.ab-item customize...nd_image, a.ab-item themes.ph...m-header, a.ab-item customize...er_image, a.ab-item edit-comments.php, a.ab-item post-new.php, a.ab-item post-new.php, a.ab-item media-new.php, a.ab-item post-new....ype=page, a.ab-item user-new.php, a.ab-item post.php?...ion=edit, a.ab-item profile.php, a.ab-item profile.php, a.ab-item profile.php, a.ab-item wp-login....68d8990f, a.screen-reader-shortcut wp-login....68d8990f, a.a2a_i.a2a_sss /, a.a2a_i.a2a_sss /, a.a2a_i.a2a_sss /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a, a.a2a_i.a2a_sss /, a.a2a_i.a2a_sss /, a.a2a_i.a2a_sss /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a.a2a_i /, a#a2apage_any_email.a2a_i.a2a_emailer /, a#a2apage_email_client.a2a_i.a2a_emailer.a2a_email_client /, a#a2apage_show_more_less.a2a_menu_show_more_less.a2a_more]

Use it to store it in an Array and then write it on the page:

var scripts = matches2array("//a");

for (i=0;i<scripts.length;i++){
document.write(scripts[i] + "\n");
}

and we got this result:

http://scheinast.eu/calculator-in-c/#content http://scheinast.eu/ http://scheinast.eu/category/c-sharp/ http://scheinast.eu/category/c/ http://scheinast.eu/category/fortran/ http://scheinast.eu/category/bash/ http://scheinast.eu/category/html-css-js/ http://scheinast.eu/category/java/ http://scheinast.eu/category/perl/ http://scheinast.eu/category/php/ http://scheinast.eu/category/projects/ http://scheinast.eu/category/regex/ http://scheinast.eu/category/tips-and-tricks/ http://scheinast.eu/category/windows/ http://scheinast.eu/perl-strict-benchmark/ http://scheinast.eu/regex-find-interpolate-strings/ http://scheinast.eu/how-to-open-remote-desktop/ http://scheinast.eu/c-calculator/ http://scheinast.eu/javascript-location/ http://scheinast.eu/image-uploader/ http://scheinast.eu/wp-admin/ http://scheinast.eu/wp-login.php?action=logout&_wpnonce=a768d8990f http://scheinast.eu/feed/ http://scheinast.eu/comments/feed/ https://wordpress.org/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ https://www.addtoany.com/share_save#url=http%3A%2F%2Fscheinast.eu%2Fcalculator-in-c%2F&title=Calculator%20in%20C%2B%2B&description=This%20is%20my%20calculator%20in%20C%2B%2B%20with%20in-line%20assembler%3A%20%5Bcrayon-556e97ccb93bc417229824%2F%5D%20Compile%20the%20code%3A%20%5Bcrayon-556e97ccb93e1826855153%2F%5D%20output%3A%20%5Bcrayon-556e97ccb93f8566053665%2F%5D... http://scheinast.eu/calculator-in-c/ http://scheinast.eu/author/paul-scheinast/ http://scheinast.eu/category/c/ http://scheinast.eu/tag/c/ http://scheinast.eu/tag/calculator/ http://scheinast.eu/wp-admin/post.php?post=1542&action=edit http://scheinast.eu/calculator-in-c/#respond http://scheinast.eu/wp-admin/profile.php http://scheinast.eu/wp-login.php?action=logout&redirect_to=http%3A%2F%2Fscheinast.eu%2Fcalculator-in-c%2F&_wpnonce=a768d8990f http://scheinast.eu/new-hompage-design/ http://scheinast.eu/backtrack-how-to/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ https://www.addtoany.com/share_save#url=http%3A%2F%2Fscheinast.eu%2Fcalculator-in-c%2F&title=Calculator%20in%20C%2B%2B%20%7C%20Paul%20Scheinast&description=This%20is%20my%20calculator%20in%20C%2B%2B%20with%20in-line%20assembler%3A%20%5Bcrayon-556e97ccb93bc417229824%2F%5D%20Compile%20the%20code%3A%20%5Bcrayon-556e97ccb93e1826855153%2F%5D%20output%3A%20%5Bcrayon-556e97ccb93f8566053665%2F%5D... http://scheinast.eu/calculator-in-c/#wp-toolbar http://scheinast.eu/wp-admin/about.php http://scheinast.eu/wp-admin/about.php https://wordpress.org/ https://codex.wordpress.org/ https://wordpress.org/support/ https://wordpress.org/support/forum/requests-and-feedback http://scheinast.eu/wp-admin/admin.php?page=wp-statistics/wp-statistics.php http://scheinast.eu/wp-admin/admin.php?page=wps_online_menu http://scheinast.eu/wp-admin/admin.php?page=wp-statistics/wp-statistics.php http://scheinast.eu/wp-admin/ http://scheinast.eu/wp-admin/ http://scheinast.eu/wp-admin/themes.php http://scheinast.eu/wp-admin/customize.php?url=http%3A%2F%2Fscheinast.eu%2Fcalculator-in-c%2F&autofocus%5Bsection%5D=themes http://scheinast.eu/wp-admin/customize.php?url=http%3A%2F%2Fscheinast.eu%2Fcalculator-in-c%2F http://scheinast.eu/wp-admin/widgets.php http://scheinast.eu/wp-admin/customize.php?url=http%3A%2F%2Fscheinast.eu%2Fcalculator-in-c%2F&autofocus%5Bpanel%5D=widgets http://scheinast.eu/wp-admin/nav-menus.php http://scheinast.eu/wp-admin/themes.php?page=custom-background http://scheinast.eu/wp-admin/customize.php?url=http%3A%2F%2Fscheinast.eu%2Fcalculator-in-c%2F&autofocus%5Bcontrol%5D=background_image http://scheinast.eu/wp-admin/themes.php?page=custom-header http://scheinast.eu/wp-admin/customize.php?url=http%3A%2F%2Fscheinast.eu%2Fcalculator-in-c%2F&autofocus%5Bcontrol%5D=header_image http://scheinast.eu/wp-admin/edit-comments.php http://scheinast.eu/wp-admin/post-new.php http://scheinast.eu/wp-admin/post-new.php http://scheinast.eu/wp-admin/media-new.php http://scheinast.eu/wp-admin/post-new.php?post_type=page http://scheinast.eu/wp-admin/user-new.php http://scheinast.eu/wp-admin/post.php?post=1542&action=edit http://scheinast.eu/wp-admin/profile.php http://scheinast.eu/wp-admin/profile.php http://scheinast.eu/wp-admin/profile.php http://scheinast.eu/wp-login.php?action=logout&_wpnonce=a768d8990f http://scheinast.eu/wp-login.php?action=logout&_wpnonce=a768d8990f http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ https://www.addtoany.com/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/ http://scheinast.eu/calculator-in-c/

10 thoughts on “JavaScript generate an result array from XPATH”

  1. Remarkable issues here. I am very happy to see your article.
    Thanks a lot and I am looking ahead to contact you.

    Will you please drop me a e-mail?

  2. This post offers clear idea designed for the new visitors of blogging, that genuinely how to do blogging.

  3. What’s up to every one, the contents present at this website are genuinely remarkable for people experience, well, keep up the good work fellows.

  4. Hey! I know this is kind of off topic but I
    was wondering which blog platform are you using for this site?
    I’m getting tired of WordPress because I’ve had issues with hackers and I’m looking at alternatives for another
    platform. I would be great if you could point me in the
    direction of a good platform.

  5. I do consider all of the concepts you have introduced for your post.
    They’re really convincing and can definitely work.
    Nonetheless, the posts are too quick for starters. May you please prolong
    them a little from next time? Thank you for the post.

  6. Ahaa, its fastidious discussion concerning this paragraph at this place at this weblog, I have read all that, so at this time me also commenting here.

  7. I do trust all the ideas you’ve presented for your post.
    They are really convincing and will certainly work.

    Nonetheless, the posts are too short for newbies. May just you please extend
    them a little from next time? Thank you for the post.

  8. Your style is unique compared to other people I have read
    stuff from. I appreciate you for posting when you have the opportunity, Guess I will just bookmark this site.

  9. Heya i’m for the first time here. I found this board and I to find It truly
    useful & it helped me out much. I’m hoping to provide
    one thing again and aid others such as you helped me.

Leave a Reply

Your email address will not be published.

five × 1 =