Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Expression "document.body.getElementsByTagName();" Vs "document.getElementsByTagName();"

Tags:

javascript

dom

Dear experts, I am quite new to javascript and I often see coders use those lines interchangeabaly.

document.body.getElementsByTagName();

and

document.getElementsByTagName();

What is the difference, benefits?

Does this have anything to do with FF and IE?

like image 462
Dennis D Avatar asked Jan 22 '23 10:01

Dennis D


1 Answers

The difference is the context. In the first example you're looking inside the body tag which means you can never retrieve the body tag itself or any other elements outside of that

In the second example example you can retrieve anything.

Nothing to do with specific browsers.

like image 184
KTastrophy Avatar answered Jan 24 '23 22:01

KTastrophy