JavaScript Document Object : links property
Description
An array containing an entry for each link in a document. The array stores an entry for each area and link object in a document; these entries are in source order. For example, if a document contains three links link-1, link-2, link-3 you can refer them as:
document.links[0]
document.links[1]
document.links[2]
You can refer to the Area and Link objects in your code by using the links array. This array contains an entry for each Area (<area href = "..."> tag) and Link (<a href = "..."> tag) object in a document in source order. It also contains links created with the link method. For example, if a document contains three links, you can refer to them as:
In the same spirit as the anchors property, you have the links property. Most pages contain several link definitions throughout the html code, as created by the <a href => tag. document.links is another object array that contains each of the links specified in the current page. Similar to document.anchors, for as many links as there are in the page, there are properties of document.links, in the following manner:
To get the number of links in a document, use the length property : document.links.length.
Version
Implemented in JavaScript 1.0
Syntax
document.link1
document.link2
........
document.linkN
Where link1, link2,.....,linkN represent the name of the links in the document.
or
document.link[0]
document.link[1]
........
document.link[n]
Where 0,1, ...n represent index count.
Example of Document object : links property
In the following web document, the links array is used to get the source of each link.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript document Object - links property</title>
</head>
<body>
<h1 style="color: red">JavaScript document Object : links Property</h1>
<h3>See the source of the each link object.</h3>
<hr />
<a href="/javascript/client-object-property-method/document-links.php">Document linkcolor Property page</a>
<br />
<a href="/javascript/client-object-property-method/client-object.php">Client Object Property method</a>
<br />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
for(i=0;i<document.links.length;i++)
{
document.write("The link object's URL is ",document.links[i].href,"</b><br />")
}
//]]>
</script>
</body>
</html>
View the example of document object's links property in the browser
Practice document object's links property with Online Editor
Supported Browser
| Internet Explorer 7 | Firefox 3.6 | Google Chrome 7 | Safari 5.0.1 | Opera 10 |
| Yes | Yes | Yes | Yes | Yes |
See also :
Please Google+, Like this tutorial on FaceBook, Tweet, save it as bookmark and subscribe with our Feed. Have suggestions? comment using Disqus down this page. Thanks.





