Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of DOM elements used in a web page

Tags:

javascript

Using jQuery I can easily get the number of DOM elements used by a web page:

$('*').length; 

But not all web sites are using jQuery.

So my question is: How do I get the number of DOM elements used in a web page using pure JavaScript and js console.

like image 532
antonjs Avatar asked Mar 21 '12 15:03

antonjs


People also ask

How do I find my DOM size?

Use offsetWidth & offsetHeight properties of the DOM element to get its the width and height.

How many DOM elements are there?

While browsers can handle larger DOM trees, they are optimized for a maximum of 32 elements deep. A large DOM tree can harm your page performance in multiple ways: Network efficiency and load performance.

How do I find the DOM element on my page?

The easiest way to find an HTML element in the DOM, is by using the element id.


1 Answers

Assuming you mean "HTMLElementNodes" as opposed to "All nodes" (which would include such things as text nodes and also be skipped by your jQuery example) then:

document.getElementsByTagName('*').length 

This still requires the use of DOM though. Pure JavaScript can't interact with an HTML document other than as a string of text.

like image 176
Quentin Avatar answered Oct 14 '22 17:10

Quentin