-1

So I was playing around with document in JavaScript, as an alternate way of using document.getElementById. But why does document.all equal null? I ran a few commands to prove it's even more 'falsy'.

document.all // HTMLAllCollection[1276]
document.all[0] // <html>...</html>

typeof document.all // 'undefined'
!{} // false. works correctly
!document.all // true! WTF

What is this? Other objects work fine. What happened to the document.all object? Is it broken?

isherwood
  • 58,414
  • 16
  • 114
  • 157
DaCuteRaccoon
  • 224
  • 2
  • 13
  • "document.all is the only falsy object accessible to JavaScript, because it has the [[IsHTMLDDA]] internal slot. This was done because of compatibility with older versions of Internet Explorer." https://developer.mozilla.org/en-US/docs/Web/API/Document/all – Đinh Carabus Feb 02 '22 at 19:43
  • It's fals**y**, not false. And it's for historical reasons. – VLAZ Feb 02 '22 at 19:43

1 Answers1

3

document.all is a non-standard API introduced by IE. It is not considered good practise to use.

For compatibility with websites that depend on it, some other browsers have introduced support for it.

Some websites that use it depend on testing for the presence of document.all to determine if the browser is IE and uses that to assume that the browser has a bunch of bugs and other non-standard APIs provided by IE.

Browsers which added support for compatibility purposes return a falsy value so that they don't get mistakenly identified as IE (and get passed other code which won't work on them).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335