最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - are document.all and document.layers obsolete now - Stack Overflow

programmeradmin4浏览0评论

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
  • 2 @Royi Namir: the latest releases of chrome and opera also support document.all – Dr.Molle Commented Apr 6, 2013 at 18:47
  • @Dr.Molle ouch....didn't know that.:-) thanks. (deleting) – Royi Namir Commented Apr 6, 2013 at 18:50
Add a comment  | 

3 Answers 3

Reset to default 14

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

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

发布评论

评论列表(0)

  1. 暂无评论