The all Collection, Inner/Outer HTML and Text, and Old and New Documents

The all collection on the document object contains references to all elements in the document page. It was a concept created by Microsoft as a way to collect all page elements into one array, before the W3C started work on standardizing the object hierarchy.

The document.all collection was one of the earlier methods that accessed individual elements; however, the actual collection itself is no longer supported in many modern browsers, such as Mozilla/Firefox. Still, the concept of being able to access any element in the document still remains; it’s just the approach that has changed. Now, you can use document.getElementById, passing in the element’s identifier to access the individual object.

Tip

In Chapter 10, you’ll see how other methods get all elements of a certain tag or, given a specific name, via the document object.

You’ll see examples of document.all in many older scripts, when it was used to differentiate object support in cross-browser DHTML applications. It’s not uncommon to see code like the following:

if (document.all)  
   elem = document.all['elemid'];
else 
   elem = document.getElementById['elemid'];

This actually works in most browsers. However, Internet Explorer is about the only browser that supports document.all now, so recognize it for what it was, but don’t use it for modern applications. IE 6.x (5.x really) supports getElementById, just like other browsers.

Another interesting item you’ll ...

Get Learning JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.