Skip to content
Related Articles

Related Articles

Display the number of links present in a document using JavaScript
  • Difficulty Level : Basic
  • Last Updated : 19 Feb, 2019

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>
<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>


Output:

2

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :