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:

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



This article is attributed to GeeksforGeeks.org

leave a comment



load comments

Subscribe to Our Newsletter