Don’t use document.all
I’ve lost count of the number of Javascript scripts I’ve seen floating around that include the equivalent of the following code snippet:
if (document.all) {
element = document.all[id];
else {
element = document.getElementById(id);
}
document.all was introduced in Internet Explorer 4, because the W3C DOM hadn’t yet standardised a way of grabbing references to elements using their ID. By the time IE 5 came out, document.getElementById() had been standardised and as a result, IE 5 included support for it.
IE 5 was released in September 1998. A popular browser statistics site (insert usual disclaimer as to the reliability of any stats but your own here) show IE 4’s market share to be in the region of 1%. Even Netscape 4 has more users than that!
Don’t use document.all. document.getElementById() is supported by every Javascript supporting browser released since 1998.
Aaron Brazell - 11th August 2003 18:51 - #
Aaron Brazell - 11th August 2003 18:52 - #
Fred Jackson - 11th August 2003 19:09 - #
They are underlined in Mozilla because they are marked up as acronyms; hover your mouse over them and their expanded form comes up as a tool tip. This is the default style for that tag as governed by the browser - IE doesn't highlight them at all. I see your point about a potential confusion with links but underlining acronyms is becoming quite common (at least in technical blogs) and I haven't yet seen a better way of visually distinguishing them from other content.
Simon Willison - 11th August 2003 19:18 - #
Poor man's getElementById for IE4 compatibility with simple scripts:
Owen - 12th August 2003 03:23 - #
Minor point: some (all?) versions of IE don't support
document.getElementsByTagName('*'), so if you want an array containing every node from a DOM tree, and you want it work in IE you still need to make use of document.all.paul hammond - 12th August 2003 10:51 - #
Simon Willison - 12th August 2003 10:53 - #
You can overwrite the existing getelementsbytagname function with one that works with '*':
function ie_getElementsByTagName(str) { if (str=="*") return document.all else return document.all.tags(str) } if (document.all) document.getElementsByTagName = ie_getElementsByTagNameShamelessly stolen from siteexperts http://www.siteexperts.com/tips/contents/ts16/page 3.asp?main=on ;)
Martijn - 12th August 2003 18:01 - #
about those acronyms..
use css to style them to look the same in IE, and make them subtle with some light gray border, and/or even dashed/dotted?
(and change a mouse pointer -- php.net does this ;)
acronym {border-bottom: 1px dashed #999;
cursor: help;
}
zombie - 12th August 2003 18:38 - #
sorry. that is better with:
border-bottom: 0.1em dashed #999;zombie - 12th August 2003 18:39 - #
Tom Morris - 13th August 2003 01:03 - #
Simon Willison - 13th August 2003 02:06 - #
Andrew Bowden - 13th August 2003 09:41 - #
Tom Morris - 13th August 2003 19:07 - #
dave - 11th September 2003 19:15 - #
I'm doing some work with popup calendar using javascript and I tried the check that dave had in his article:
if(document.all && !document.getElementByID) {
alert("document.all supported\ndocument.getElementByID not supported");
}
I'm using IE6 and for some reason this alert always pops up. I can't figure out why document.getElementByID would not be supported by IE 6. Any suggestions? Thanks.
Matt - 15th September 2003 16:49 - #
document.getElementByIdit works fine. In any case, unless you absolutely have to support IE 4 (which has a market share statistically approaching zero) you should forgetdocument.alleven exists and just usedocument.getElementById.Simon Willison - 15th September 2003 17:11 - #
loufoque - 16th November 2003 21:00 - #
R. MacAran - 11th December 2003 17:17 - #
getElementById has worked in Mozilla for years and still does, it's W3 DOM not 'IE stuff'.
Dave - 23rd February 2004 23:49 - #
var checkBoxes = document.getElementById('CheckBox');
// I have more than one object named "CheckBox" in the page, I am suppose to receive an array.
var numberTables = checkBoxes.length;
//numberTables return 'undefinded'
Help please.
Lee - 22nd March 2004 22:45 - #
IDs should be unique for the page on which they are used. To grab all check boxes, use something like this (untested):
var inputs = document.getElementsByTagName('input'); var checkboxes = []; for (var i = 0; i < inputs.length; i++) { if (inputs[i].type == 'checkbox') { checkboxes.append(inputs[i]); } }Simon Willison - 22nd March 2004 23:02 - #
Matt - 2nd April 2004 22:47 - #
dgretlein - 21st May 2004 05:59 - #
jeroen schellekens - 16th June 2004 21:22 - #
document.getElementById(id);has exactly the same effect. I'll take the one line version over the longer version any day. IE4 support just isn't an issue any more.Simon Willison - 16th June 2004 21:52 - #
Stan - 1st July 2004 15:17 - #
Stan,
If you check the W3C documentation, you'll see that IDs are supposed to be unique within a page. In other words, you should not be using multiple div's with the same ID, that what CSS classes are for. That's why there is no 'getElementsById' but instead we have 'getElementsByName' which, "Returns the (possibly empty) collection of elements whose name value is given by elementName.". Hope this helps.
Jason - 9th July 2004 16:06 - #
CC - 8th October 2004 21:10 - #
In response to dgretlein's post, to change the css class using javascript, try the following:
Gareth - 24th October 2004 06:18 - #
Troff - 18th May 2005 15:33 - #
Cailean - 1st June 2005 05:29 - #
Murty - 1st June 2005 12:51 - #
KarlB - 7th July 2005 19:14 - #
KarlB: That's what document.getElementsByTagName (and the other DOM standard navigation methods, such as element.children, element.firstChild etc) are for. document.getElementsByTagName('*') will give you all of the elements on a page.
Admittedly, this doesn't work in older versions of IE - in which case you have to fall back on document.all. That's the only case in which it should be used, and you use it like this:
var elements = document.all ? document.all : document.getElementsByTagName('*');
To ensure compatibility with non-broken browsers.
Simon Willison - 7th July 2005 20:29 - #
I read all the way through these comments hoping someone would mention an alternative to something I came across in IE specific code. Hopefully I'll find the answer sooner or later, but the code was iterating through elements with x and checking if x index's ID had a particular name, so:
for (x=0; x<document.all.length; x++) if(document.[x].id=="textanimlink")Any ideas on a simple way to convert this to work in non-IE browsers (FireFox)?
Mardeg - 29th July 2005 20:42 - #
Gustavo Gonzalez - 29th July 2005 22:28 - #
timbuck - 6th December 2005 05:03 - #
mamo - 4th January 2006 19:16 - #
Greg Hinch - 3rd February 2006 02:16 - #
sonali,BTech 2001 - 29th March 2006 08:20 - #
sonali,BTech 2001 - 29th March 2006 08:23 - #
Sharon Tam - 13th April 2006 02:06 - #
Volte - 2nd May 2006 03:39 - #
Hey, I've been googling for days for solutions of this, very little information out there, this is by far the most informative. Ironically I started making my own function for getElementsById, and getElementsByClass (because, I tried that getElementsByName, doesnt work the way you think it does, that actually gets elements with the tag name.) So, in the name of science, programming, and furthering out knowledge, I'll share my code, its not complex, but its efficient. Works in Safari, and Firefox. Not sure about the other browsers. I don't have Internet Explorer, someone will have to test that POS.
Volte - 2nd May 2006 14:21 - #
Nick Linnear - 2nd May 2006 19:43 - #
if (document.layers) { type="NN"; } else { if (document.all) { if (document.getElementById) { type="IE5+"; } else { type="IE4"; } } else { type="FF"; } }Danny Bishop - 4th May 2006 01:40 - #
Oliver Kiss - 26th May 2006 04:53 - #
Justin Megawarne - 11th September 2006 12:08 - #