- Why Does !document.all Return True in JavaScript
- Uncovering the reason why this code returns true and what it means for your code
- A Brief History of JavaScript and DOM
- Understanding document.all
- Type Coercion in JavaScript
- What does document.all mean? [duplicate]
- What does document.all mean? [duplicate]
- Are document.all and document.layers obsolete now
- Why is document.all falsy?
- ES2019 Update
- Sample 1
- Sample 2
- JavaScript: document.all.item() replacement
- Document: all property
- Value
- Special type conversion behavior
- Specifications
- Browser compatibility
- Found a content problem with this page?
Why Does !document.all Return True in JavaScript
Uncovering the reason why this code returns true and what it means for your code
JavaScript is an incredibly versatile and widely-used programming language. As with any language, it has its quirks, and one of the puzzlings is the behavior of the !document.all expression.
A Brief History of JavaScript and DOM
To understand the context of !document.all , we must first take a brief look at the history of JavaScript and the Document Object Model (DOM). JavaScript was created by Brendan Eich in 1995, and it was initially developed for the Netscape Navigator browser. The language was designed to be easy to use and learn, but it was also developed quickly, leading to some quirks that persist today.
The DOM is a tree-like representation of an HTML document, allowing JavaScript to manipulate the elements and their attributes. Early browsers, such as Netscape Navigator and Internet Explorer, had their own implementations of the DOM, which led to compatibility issues. One such feature was the document.all collection, introduced by Internet Explorer.
Understanding document.all
document.all is a non-standard feature that was introduced by Microsoft in Internet Explorer 4.0 to access all elements in an HTML document. It predates the standardized document.getElementById and document.querySelector methods that are now commonly used. The document.all collection was later adopted by other browsers for compatibility reasons, but its use is discouraged in favor of standard DOM methods.
An example of using document.all is as follows:
Type Coercion in JavaScript
JavaScript is a loosely typed language, meaning variables can hold values of any data type without any prior declaration. This…
What does document.all mean? [duplicate]
To make browser detection possible (back in the old days you could tell IE apart from NN by testing for ) while supporting document.all syntax, other browsers made the «weird» implementation that returns undefined. In modern browsers, we’d prefer to use , of course, but since most browsers still have (for other backwards compatibility reasons)
What does document.all mean? [duplicate]
I’m refactoring some old code written by somebody else. And I came across the following snippet:
if (document.all || document.getElementById)
When will the code within the if-statement be executed?
document.all() is a non-standard way of accessing DOM elements. It’s been deprecated from a few browsers. It gives you access to all sub elements on your document.
document.getElementById() is a standard and fully supported. Each element has a unique id on the document.
Javascript — What does document.all mean?, document.all () is a non-standard way of accessing DOM elements. It’s been deprecated from a few browsers. It gives you access to all sub elements on your document. document.getElementById () is a standard and fully supported. Each element has a unique id on the document. Share.
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.
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.
Javascript — How to get the entire document HTML as a, You have to iterate through the document childNodes and getting the outerHTML content. in VBA it looks like this. For Each e In document.ChildNodes Put ff, , e.outerHTML & vbCrLf Next e using this, allows you to get all elements of the web page including < !DOCTYPE >node if it exists
Why is document.all falsy?
document.all is a non-primitive object in the DOM that is falsy.
For example, this code doesn’t do anything:
Can someone explain why this is?
Disclaimer: I’m the guy who tweeted the question that led to this thread 🙂 It was a question I would ask and answer in my Front-Trends talk. I wrote that tweet 5 minutes before going on stage.
The question I was asking is the following.
The ECMAScript spec defines ToBoolean() as follows:
As you can see, all non-primitive objects (i.e. all objects that aren’t a boolean, a number, a string, undefined , or null ) are truthy as per the spec. However, in the DOM, there is one exception to this — a DOM object that is falsy. Do you know which one that is?
The answer is document.all . The HTML spec says:
The all attribute must return an HTMLAllCollection rooted at the Document node, whose filter matches all elements.
The object returned for all has several unusual behaviors:
The user agent must act as if the ToBoolean() operator in JavaScript converts the object returned for all to the false value.
The user agent must act as if, for the purposes of the == and != operators in JavaScript, the object returned for all is equal to the undefined value.
The user agent must act such that the typeof operator in JavaScript returns the string ‘undefined’ when applied to the object returned for all .
These requirements are a willful violation of the JavaScript specification current at the time of writing (ECMAScript edition 5). Therequires that the ToBoolean() operator convert all objects to the true value, and does not have provisions for objects acting as if they were undefined for the purposes of certain operators. This violation is motivated by a desire for compatibility with two classes of legacy content : one that uses the presence of document.all as a way to detect legacy user agents, and one that only supports those legacy user agents and uses the document.all object without testing for its presence first.
So, document.all is the only official exception to this ECMAScript rule. (In Opera, document.attachEvent etc. are falsy too, but that’s not specced anywhere.)
The above text explains why this was done. But here’s an example code snippet that’s very common on old web pages, and that will illustrate this further:
if (document.all) < // code that uses `document.all`, for ancient browsers >else if (document.getElementById) < // code that uses `document.getElementById`, for “modern” browsers >
Basically, for a long time document.all was used in this way to detect old browsers. Because document.all is tested first though, more modern browsers that offer both properties, would still end up in the document.all code path. In modern browsers, we’d prefer to use document.getElementById , of course, but since most browsers still have document.all (for other backwards compatibility reasons) the else would never be accessed if document.all was truthy. Had the code been written differently, this wouldn’t be a problem:
if (document.getElementById) < // code that uses `document.getElementById`, for “modern” browsers >else if (document.all) < // code that uses `document.all`, for ancient browsers >
But sadly, a lot of existing code does it the other way around.
The simplest fix for this problem is to simply make document.all be falsy in browsers that still mimic it.
ES2019 Update
There is now an [[IsHTMLDDA]] internal slot for objects:
An [[IsHTMLDDA]] internal slot may exist on implementation-defined objects. Objects with an [[IsHTMLDDA]] internal slot behave like undefined in the toboolean and Abstract Equality Comparison abstract operations and when used as an operand for the typeof operator.
The HTML Standard has also been updated to add that internal slot for objects that implement the HTMLAllCollection interface:
Objects that implement the HTMLAllCollection interface are legacy platform objects with an additonal [[Call]] internal method described in the section below. They also have an [[IsHTMLDDA]] internal slot.
The reason for this madness is specified in this note in the HTML Standard:
These special behaviors are motivated by a desire for compatibility with two classes of legacy content: one that uses the presence of document.all as a way to detect legacy user agents, and one that only supports those legacy user agents and uses the document.all object without testing for its presence first.
So basically the standard wants to be compatible with these two types of code:
Code that checks if it is running inside Internet Explorer to use its non-standard features, like document.all and Activex;
document.all["my-button"].onclick = function () < alert("hi"); >;
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
In short, it’s to make BOTH of these code samples work. Browsers have to do this so that old web pages will continue to work.
Sample 1
// Internet Explorer if (document.all) < useActiveX() >// Netscape Navigator else
Sample 2
document.all.output.innerHTML = 'Hello, world!'
Get all elements in the body tag using pure javascript, Sorted by: 103. If you want all elements inside the body tag, not just first level children, you can simply use getElementsByTagName () with a wildcard: var elems = document.body.getElementsByTagName («*»); Share. Improve this answer. answered Oct 10, 2012 at 16:06. jfriend00. 643k 88 906 915.
JavaScript: document.all.item() replacement
I am using document.all.item(«name») and it works in IE 10 and Google Chrome 29 but not in Firefox. Is there a replacement that is compatible with Firefox?
document.getElementsByName(«name») should do the same thing, but better because it handles the case where there are multiple elements with the same name properly (ie. radio buttons, form arrays, etc.)
I know this is pretty old thread which I happened to stumble upon today. A fact we need to consider in replacing document.all.item with document.getElementByName is that former returns HTMLCollection while latter returns NodeList . Here is another SO thread discussing the differences between these two.
HTML DOM Document querySelectorAll() Method, Definition and Usage. The querySelectorAll () method returns all elements that matches a CSS selector (s). The querySelectorAll () method returns a NodeList. The querySelectorAll () method throws a SYNTAX_ERR exception if …
Document: all property
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
The Document interface’s read-only all property returns an HTMLAllCollection rooted at the document node. In other words, it returns all of the document’s elements, accessible by order (like an array) and by ID (like a regular object).
Value
An HTMLAllCollection which contains every element in the document.
Special type conversion behavior
For historical reasons, document.all is an object that in many ways behaves like undefined . Specifically:
These special behaviors ensure that code like:
if (document.all) // Assume that we are in IE; provide special logic > // Assume that we are in a modern browser
Will continue to provide modern behavior even if the code is run in a browser that implements document.all for compatibility reasons.
However, in all other contexts, document.all remains an object. For example:
- It is not strictly equal to either undefined or null .
- When used on the left-hand side of the nullish coalescing operator ( ?? ) or the optional chaining operator ( ?. ), it will not cause the expression to short-circuit.
Specifications
Browser compatibility
BCD tables only load in the browser
Found a content problem with this page?
This page was last modified on Jun 20, 2023 by MDN contributors.
Your blueprint for a better internet.