Display the number of links present in a document using JavaScript

Any webpage that is loaded in the browser can be represented by the Document interface. This serves as an entry point to the DOM tree and the DOM tree contains all elements such as <body> , <title>, <table> ,<a> etc.

We can create a Document object using the Document() constructor. There are many properties of these Document object. One of such property is links. The links property gives us a collection of all <area> elements and <a> elements in a document. One more property is the length property. The length property tells us the number of links present in the document.links array. So, the document.links.length statement gives us the number of links present in a document.

Below HTML document contains JavaScript piece of code which tells the number of links present in the document:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
  
<a href = "www.geeksforgeeks.org"></a>
<a href="practice.geeksforgeeks.org"></a>
  
  
<script type="text/javascript">
  document.write("Number of links: " + 
                  document.links.length);
</script>
  
</body>
</html>

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Lets get started

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.