Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document.all vs. getelementbyID

Status
Not open for further replies.

fuli42

Technical User
Jul 9, 2002
74
HU
Formely I've been using document.layers (NN4) and document.all (IE) for browser conditioning.
On Mozilla (no more layers), some of my scripts are not working.
How should I do the transition form document.all to getelementbyID, what are the differences, support?

Any sites recommended?

Thanks,

Fuli42

 
hi Fuli42,

document.getElementById() is DOM standard. Any browser from now on should support it. Currently supported by IE 5+ (maybe even 4?), NS6+, Mozilla, and probably many others - any Opera or "others" people out there?

=========================================================
if (!succeed) try();
-jeff
 
I don't think IE 4 supports document.getElementById, but I could be wrong. (so I heard)

>How should I do the transition form document.all to getelementbyID, what are the differences, support?
Are you asking for how getElementById works?
If so, here's how the two compare:
document.all["name_of_something"].value
document.getElementById("name_of_something").value
they both access the same thing.
 
Hi fuli42,
Basically what jemminger and KevinAr18 said is true;
NS4 DOM is document.layers, IE4 is document.all, IE5 is document.all and document.getElementByID, and NS6 and other browsers are the W3C standard, document.getElementByID.
Stats at my sites still show a certain number of NS4 and IE4 users, so when I design scripts, I try to accomodate those browsers to a certain degree. I use a variant of a "cross-browser DOM" script I found in the Quickstart series, "DHTML and CSS for the World Wide Web" published by Peachpit press. I found this book to be a succint,helpful, and inexpensive guide when I was getting familiar with the ID DOM statndard. A typical variant of their script that I use is below:


d=document;
ID=(d.getElementById)?1:0;
ALL=(d.all)?1:0;

/*
you have to use this type of browser detection for NS4, because "document.layers" doesn't work in external script libraries for this browser
*/

NS4=(navigator.appName.indexOf("Netscape")!=-1)&&(navigator.appVersion.indexOf("4")!=-1);
IE5=ID&&ALL;
NS6=ID&!ALL;

/*
This function returns the various DOM references for style and non-style related objects.
*/

function DOM(objID,style){
if(style){
if(ID){
return (d.getElementById(objID).style);
}
if(ALL&!ID){
return (d.all[objID].style);
}
if(NS4){
return (d.layers[objID]);
}
}else{
if(ID){
return (d.getElementById(objID));
}
if(ALL&!ID){
return (d.all[objID]);
}
if(NS4){
return (d.layers[objID]);
}
}
}

/*
then, you can call this in other functions to supply the correct type of DOM reference without having to use multiple "if else" type conditionals. You might use this to say, change the visibilty if a style element with an ID of "example" like this:
*/

function showDiv(objID,state){
DOM(objID,1).visibility=state;
}

// and call it like this:

onmouseover="showDiv('example','hidden');"

The quickstart book also has a variant of this to deal with nested layers in NS4. Of course, there are lots of other little details of differing stylesheet support and HTML/JavaScript syntax when you are trying to adapt a single script to be "cross-browser" compatible, so the script above only does so much, but I find it to be a real time saver.
Hope this helps.
sundemon
 
Thanks for the info.

So if i understood it right, you can safely replace document.all with document.getElementById, and be forward-compatible.
There are now very few people who are still using IE4. For NN4, i will still continue to condition my scripts.

Sundemon: Thanks for the script! I've found a very similar script in function, on webmonkeys site (javascript libraries). (You run it with an ID and it gives you an object reference for your browser.
THere is a 'full version', that works with nested divs. If you have to refer to an element contained on a layer for example, the above script will not work on NN4.


Cheers,

Fuli42
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top