JavaScript returns the number of hyperlinks in a web page

  • 2020-05-26 07:43:02
  • OfStack

This example shows how JavaScript returns the number of hyperlinks in a web page. Share with you for your reference. The details are as follows:

The JS code below gets the number of hyperlinks by getting all the hyperlinks in the page through document.links


<!DOCTYPE html>
<html>
<body>
<img src ="planets.gif" width="145" height="126" 
alt="Planets" usemap ="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,78,125" href="sun.htm" alt="Sun">
<area shape="circle" coords="88,60,5" href="mercury.htm" alt="Mercury">
<area shape="circle" coords="125,60,9" href="venus.htm" alt="Venus">
</map>
<p><a href="/js/">JavaScript Tutorial</a></p>
<p>Number of areas/links:
<script>
document.write(document.links.length);
</script></p>
</body>
</html>

The results are as follows:


Number of areas/links: 4

I hope this article has been helpful to your javascript programming.


Related articles: