Helpful Information
 
 
Category: DOM and JSON scripting
DOM Object

What is document.all used for?:confused:

Long time ago before The Great Rain of Rocks™, there were many opinions (point of views), how html-document should be handled as an object. (thinking: jkd probably explains this better due to my poor skill of English, but I still try)

IE4 : document.all[objectID]
NN4 : document.layers(layerName)
DOM : document.getElementById(objectId)

Follow the conversation here in forums, and you'll learn basics of DOM in no time. That's the best way to get yourself familiar with document object modelling. Also, visit W3C Homepage (http://www.w3.org) for exact DOM Core Recommendations.

document.all was an interesting, proprietary object introduced by MS in IE4 to compete with NS4's Layer model.

It is really an array, but since MS liked VBScripter's, they also allowed the use of () indexing, like in VB.

Which means you had several ways to use it:

document.all['someIDorName']
is the same as:
document.all('someIDorName')

Since more than one element is allowed the same name attribute, sometimes you get a collection returned to you:

document.all('someName').length > 1

Which meant you either went like:

document.all('someName')[X]
OR, since VBScript has a special syntax for nested arrays:
document.all('someName', X);

Where X is the offset of the node in the collection.

If that isn't enough, you can specify a numerical index in the all array:

document.all[0] // document.all(0)

returned the very first node in the document for example.

On top of all that, you could also get collections of all nodes with the same tag:

document.all.tags('tagname')

Then you access the offset with array notation. You can also access elements the proper way from collections with item(X), though I'm not sure if IE4 had that method.


All those abilities are covered in the W3C DOM1 Core, so there is no need to use that notation anymore:

Get an element by its id:
document.getElementById('id');


Get elements by their name:
document.getElementsByName('name')
That returns a collection, so use item(X) or [X] notation to get it

Get a collection of tags:
Node.prototype.getElementsByTagName('tagname')

I say Node.prototype because all nodes have this method, and return only childnodes of that node.
The most typical use is
document.getElementsByTagName('bla');
This method returns a collection once again.

That aid any? :)

Also, I think I'm going to move this to the DOM Scripting forum, even though it is about document.all, one could argue that is an older version of a DOM.

So, document.all is used by IE4, document.layer by NS4, and newer browsers (that are W3C-compliant) use document.getElementById (or getElementsByName or getElementsByTagName). That is correct, right?

So, if I want to change something about a DIV with the id, MyDiv for example, such as perhaps its innerHTML or its display style, I need to create a variable, divToChange for example, with the path to the div depending on which browser is used. How would I test for which method to use and then use it. I assume "if () { } if () { } else {}" is needed but what goes in the normal brackets?

var div;
if (typeof document.layers != 'undefined') div = document.layers['MyDiv'];
else if (typeof document.all != 'undefined') div = document.all['MyDiv'];
else if (typeof document.getElementById != 'undefined') div = document.getElementById('MyDiv');


That is really the long way around it, and is a prime example of why people need to upgrade to standards compliant browers.

You can shortcut around NS4 though if you don't want to support it:

var div = (document.getElementById || document.all)('MyDiv');










privacy (GDPR)