Paul
Paul

Reputation: 1614

are document.all and document.layers obsolete now

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.

Upvotes: 11

Views: 14178

Answers (3)

Mark Lloyd
Mark Lloyd

Reputation: 41

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('*')

Upvotes: 0

Guffa
Guffa

Reputation: 700690

Yes, 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

Upvotes: 14

ZER0
ZER0

Reputation: 25332

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.

Upvotes: 2

Related Questions