The links
collection returns an array of
all the links in the current document.
document.links[].property
links |
Yes | Yes | Yes | Yes | Yes |
The following code shows how to get the number of links in the document:
<!DOCTYPE html>
<html>
<body>
<p><a href="#">test</a></p>
<script>
document.write(document.links.length);
</script></p>
</body>
</html>
The code above is rendered as follows:
The following code shows how to get the id of the first link in the document.
<!DOCTYPE html>
<html>
<body>
<p><a id="javascript" href="#">test</a></p>
<!-- ww w.ja va 2 s . c o m-->
<p>Id of first area/link:
<script>
document.write(document.links[0].id);
</script></p>
</body>
</html>
The code above is rendered as follows: