
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to find the number of links in a document in JavaScript?
In this article we are going to discuss how to find the number of links in a document in JavaScript.
The DOM property in the JavaScript provides many properties like <head>, <title>, <body>, <link>, <a>, <style>. There are two ways through which we can represent the links in HTML DOM. The DOM object provides links property.
The document.links property provides us a collection of <a> and <area> tags inside a document. The document.links property is similar to an array. We can access any property (like href, name, title) of the links using this property. To find the total number of links, the length property is used. The document.links.length gives us the total number of links in a document.
Syntax
The syntax to determine the number of links in a document is shown below.
document.links.length
Example 1
The following is an example program to find the number of links in a document.
<html> <body> <a href="https://www.tutorialspoint.com/javascript/index.htm">Javascript</a><br> <a href="https://www.tutorialspoint.com/php/index.htm">Php</a><br> <a href="https://www.tutorialspoint.com/java8/index.htm">Php</a><br> <p id="links"></p> <script> document.getElementById("links").innerHTML = "The number of links are: " + document.links.length; </script> </body> </html>
On executing the above code, the following output is generated.
Example 2
The following is another example program to find the number of links in a document.
<!DOCTYPE html> <html> <head> <title>To find the number of links in a document in JavaScript</title> </head> <body style="text-align : center"> <h1>To find the number of links in a document in JavaScript</h1> <a name="google" href="https://www.google.com/" title="Google Home Page">Google</a><br> <a name="facebook" href="https://www.facebook.com/" title="Facebook Home Page">Facebook</a><br> <a name="youtube" href="https://www.youtube.com/" title="Youtube Home Page">Youtube</a><br> <p id="text1"></p> <script> document.getElementById("text1").innerHTML = "The total number of links in the document are : "+document.links.length; </script> </body> </html>
On executing the above code, the following output is generated.
Example 3
The following example tells us about each link and count of the total number of links.
<!DOCTYPE html> <html> <head> <title>To find the number of links in a document in JavaScript</title> </head> <body style="text-align : center"> <h1>To find the number of links in a document in JavaScript</h1> <a name="google" href="https://www.google.com/" title="Google Home Page">Google</a><br> <a name="youtube" href="https://www.youtube.com/" title="Youtube Home Page">Youtube</a><br> <p id="text1"></p> <script> var result=""; for(var i=0;i<document.links.length;i++){ result += "Link "+(i+1)+" : "+document.links[i].href+"<br/>"; } document.getElementById("text1").innerHTML = "The links in the document are : "+'<br/>'+result+'<br/>'+"The count for total number of links is : "+document.links.length; </script> </body> </html>
On executing the above code, the following output is generated.
- Related Articles
- How to show the number of links in a document with JavaScript?
- How to find a number of anchors in a document in JavaScript?
- How to return the number of images in a document with JavaScript?
- How to get the number of forms in a document with JavaScript?
- How to find the href attribute of a link in a document in JavaScript?
- How to find the name of the first form in a document with JavaScript?
- How to count the total number of links in a page in Selenium?
- How to count number of keys in a MongoDB document?
- How to count the total number of links in Selenium with python?
- How to find the cube root of a number in JavaScript?
- How to get the title and full URL of a document in JavaScript?
- How to get a particular anchor in a document in JavaScript?
- How to get the cookies associated with a document in JavaScript?
- How to use JavaScript to replace the content of a document with JavaScript?
- How to display the title of a document with JavaScript?
