Before there were standards for HTML, CSS and javascript who defined this were the browsers, ie each browser had its own way of doing things, as for example each had a way of catching elements of DOM, for example:
- Internet Explorer 4 used
document.all
- Netscape4 wore
document.layers
These were the main "market" browsers and were the ones that first implemented Javascript (in IE it was called Jscript)
Since the IE5.01 version, the document.getElementById
and document.getElementsByTagName
, the only one that didn’t work in IE was the document.getElementsByTagName("*");
, that only came to be supported in IE6, for that we had to use document.all
yet.
The Ids because they are "unique" elements in IE (although some developers repeat them, which is a mistake) became accessible directly in the references of global variables that in Javascript for browsers is the object window
, provided that the DOM was loaded and the variable had not been declared.
IE maintained this feature to maintain compatibility with sites that had been made for previous versions, some browsers imported this feature to try to maintain compatibility
My opinion: at the time it was very common to accuse the browser for not working on a given site, when the problem was in the script, so many engine developers were forced to put these peculiarities in the engines themselves.
In Firefox for example document.layers
was still supported, but issued a Warning on the console warning that it would be removed, unlike the Internetexplorer kept many things, such as the document.all
(this sometimes generates a DOM list other than document.getElementsByTagName("*");
).
There was an engine called iCab (until version 3 of the browser whose engine name was also called iCab, in version 4 onwards it went on to use Webkit and Cocoa API) for Macos which had similar functionalities to IE4 like document.all
, This was a way to maintain compatibility with websites made for Internet Explorer.
It is likely (has nothing discussed, it is just an assumption) that with the evolution of browsers and "standards" of Ecmascript the window
generate a variable that represents an element by id automatically is removed one day and only what is really needed is maintained, that is if you use something like window.meuElemento
, this may fail in the future.
I also don’t recommend using the ID reference because of conflicts with variables that may have been defined and that have the same name as the id
(this is a problem that can already occur and does not depend on the future).
Note that there are other properties to get DOM elements like:
document.forms
Get forms by index as document.forms[0]
document.embeds
Obtains elements <embed>
by the index as document.embeds[0]
window.frames
Obtains elements <frame>
and <iframe>
by the index as window.frames[0]
, note that window.frames
does not return the DOM element of (i)frame but the object window
from within, this window.frames[0]
then it would be the same as doing this document.getElementsByTagName('iframe')[0].contentWindow
Now with this note from W3C I say that this is the best answer +1
– Guilherme Nascimento