GetElementsByTagName ("div"). length returns zero for any web page

I am trying to develop a Firefox extension. The following code:

var divList = document.getElementsByTagName("div") ; Components.utils.reportError("num of divs = " + divList.length) ; 

Always says: - the number of divs = 0.

I do not understand what the error is that I am making in this simple piece of code. When I replace the "div" with "*" , I always get a value of about 1100 on any web page.

+3
source share
2 answers

You don't know what the difference is in the context of the extension, but it may be related to namespaces - https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName . What is the tag of the first of 1100 elements that you get when using "*"?

0
source

Maybe the code runs when the DOM is not ready.

Try putting the JS code inside the script tag at the very end of the body tag.
I'm not sure why * gets so many items.

+4
source

Source: https://habr.com/ru/post/887033/


All Articles