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> <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
Recommended Posts:
- Where to put JavaScript in an HTML Document ?
- How to get the entire HTML document as a string in JavaScript ?
- How to write shorthand for document.getElementById() method in JavaScript ?
- How to disable HTML links using JavaScript / jQuery ?
- Open all persons solution links from submission page using JavaScript
- How do you display JavaScript datetime in 12 hour AM/PM format?
- How to display confirmation dialog when clicking an <a> link using JavaScript / jQuery ?
- How to display hidden element when a user starts typing using JavaScript ?
- How to store all dates in an array present in between given two dates in JavaScript ?
- How to get the elements of one array which are not present in another array using JavaScript?
- CSS | @document rule
- DOM (Document Object Model)
- What is the difference between display: inline and display: inline-block in CSS?
- How to convert a PDF document to a preview image in PHP?
- How to convert an HTML element or document into image ?
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.

