Something very small thing that JavaScript can show – Search and count up Nodes in document –


Search and count up Nodes in document.

Example, an HTML file shows it contains five sets of tags in the file.

That search and count up have done by the following a few JavaScript lines.

<!DOCTYPE html>
     function countTags(n){ 
           var numtags = 0; 
           if (n.nodeType == 1 /*Node.ELEMENT_NODE*/){ 
                 var children = n.childNodes; 
           for (var i=0; i < children.length; i++){ 
                 numtags += countTags(children[i]); 
           return numtags;
<body onload="alert('this document has' + countTags(document) + ' tags')">
This is a <i>sample</i> document.