I am going through some (old?) native javascript and I encountered a separation of document.getElementById, document.all and document.layers.
From what I know, document.all and document.layers are obsolete now, but I just wanted to make sure.
I am going through some (old?) native javascript and I encountered a separation of document.getElementById, document.all and document.layers.
From what I know, document.all and document.layers are obsolete now, but I just wanted to make sure.
Share Improve this question edited Apr 6, 2013 at 19:11 Paul asked Apr 6, 2013 at 18:28 PaulPaul 1,6145 gold badges18 silver badges24 bronze badges 2 |3 Answers
Reset to default 14Yes, they are obsolete.
The document.all
collection is specific to Internet Explorer. The document.layers
collection was specific to Netscape. Neither is in the standards.
Today we use document.getElementById
instead.
See also: https://developer.mozilla.org/en-US/docs/Mozilla_Web_Developer_FAQ#JavaScript_doesn.E2.80.99t_work.21_Why.3F
Yes, they are. They comes from a period where Internet Explorer 4 and Netscape 4.x were the main browsers: document.layers
was used by Netscape, and document.all
from IE. The first is definitely unused anymore, where I guess document.all
is still used for legacy in IEs.
document.all is supported by most (if not all) modern browsers. It is just considered false if you try to test for it, but will work if you use it (IIRC, they wanted to discourage people from using it to test for IE).
To really test for it you can use try .... catch. What I tested was:
var da = document.all.length;
which throws an error if document.all isn't supported.
The modern equivalent of document.all is document.getElementsByTagName('*')
document.all
– Dr.Molle Commented Apr 6, 2013 at 18:47