Tony Tony - 1 year ago 67
Javascript Question

Why link numbers are different?

I tried to count links within a page using JS, but got different results. Why there is a difference?

var intLNK = document.links.length;

var intA = document.getElementsByTagName("a").length;

Answer Source

Quoting from MDN

The links property returns a collection of all <area> elements and <a> elements in a document with a value for the href attribute.


will return the anchor elements irrespective of href attribute. You may use


to get the number of anchors having href attribute.

If you're interested in performance of two see to Robert Weber

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download