HTML DOM linksCollection
Document Object
Definition and Usage
The links collection returns an array of all links in the current document.
Tip:The links collection counts <a href=""> tags and <area> tags.
Syntax
document.links[].property
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the links collection
Examples
Example 1
Return the number of links in the document:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm.html" alt="Venus">
</map>
<p><a href="../js/index.html">JavaScript Tutorial</a></p>
<p>Number of links/addresses:
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm.html" alt="Venus">
</map>
<p><a href="../js/index.html">JavaScript Tutorial</a></p>
<p>Number of links/addresses:
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
The above example outputs:
Number of links/addresses: 4
Try it yourself »
Example 2
Return the first link of the document:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area id="sun" shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Sun">
<area id="mercury" shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercury">
<area id="venus" shape="circle" coords="124,58,8" href="venus.htm.html" alt="Venus">
</map>
<p><a id="javascript" href="../js/index.html">JavaScript Tutorial</a></p>
<p>The ID of the first link/address:
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial (example.com)</title>
</head>
<body>
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area id="sun" shape="rect" coords="0,0,82,126" href="sun.htm.html" alt="Sun">
<area id="mercury" shape="circle" coords="90,58,3" href="mercur.htm.html" alt="Mercury">
<area id="venus" shape="circle" coords="124,58,8" href="venus.htm.html" alt="Venus">
</map>
<p><a id="javascript" href="../js/index.html">JavaScript Tutorial</a></p>
<p>The ID of the first link/address:
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
The above example outputs:
The ID of the first link/address: sun
Try it yourself »
Document Object Other Extensions