Skip to content

Get total number of links from a HTML document using JavaScript

All links in a HTML document is stored in document.links as a HTMLCollection. You can get the total number of links in a HTML web page like:

Advertisements
console.log(document.links.length); //268

Similar to PluginArray object, HTMLCollection is special type of array which has the length property and supports iterating it using traditional for loop and using index value to get the values.

console.log("All links in the page");
console.log("-----------------");
for(var i = 0; i < document.links.length; i++) {
  console.log("Link " + i + " - " + document.links[i]);
  console.log("-----------------");
}

// Link 1 - https://poopcode.com/list-down-all-the-plug-in-installed-in-your-browser/

.
.
.
.
.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.