Why is document.all falsy?

前端 未结 4 1431
难免孤独
难免孤独 2020-11-29 21:52

document.all is a non-primitive object in the DOM that is falsy.

For example, this code doesn\'t do anything:

if (document.all) {
    al         


        
4条回答
  •  自闭症患者
    2020-11-29 22:20

    Modern browsers don't implement this outdated thing any more. It was introduced by IE, but most of the others "shim" it to be compatible.

    To make browser detection possible (back in the old days you could tell IE apart from NN by testing for document.all) while supporting document.all syntax, other browsers made the "weird" implementation that typeof document.all returns undefined.

    Opera> document.all
    // prints the array-like object
    Opera> typeof document.all
    "undefined"
    Opera> Boolean(document.all)
    false
    

    Before FF dropped support for it, it also showed weird behaviour as stated in this message. You may find more internals in Mozilla bug #412247.

    There is also a very long thread in the W3C mailing list archive, beginning with http://lists.w3.org/Archives/Public/public-html/2009Jun/0546.html

提交回复
热议问题