"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:he**********@hotpop.com...
<snip>
This question comes up a lot. Is it time for an entry in the FAQ?
This is another of those cases where the question itself is not really
asked often but the information is very frequently included as part of
answers. That would suggest that maybe it should be in the FAQ (as other
entries (eval (#FAQ4_40), for example) got in by the same criteria).
<FAQENTRY>Why doesn't the global variable "divId" refer to
the element with id="divId"?
I wonder whether that question shouldn't be worded "Why doesn't the
global variable "divId" always refer to the element with id="divId"?" -
or - "Why doesn't the global variable "divId" refer to the element with
id="divId" in all browsers?" (though the latter would mean re-wording
the opening sentence).
It does in Internet Explorer, but not in *many* other
We could quibble about "*many*" as reproducing this IE feature seems to
be quite common among modern browsers, Mozilla/Gecko browsers being
primarily the ones that render this IE shortcut invalid in cross-browser
scripting.
browsers. The recommended way of referring to an element
with id="foo" is
document.getElementById("foo")
In order to support older browsers that doesn't implement
this W3C DOM method, fallback to proprietary features can be
used. In Internet Explorer 4 (and WebTV?), you can use
document.all["foo"] . In Netscape 4, maybe you can use
document.layers["foo"] , but only if the element is absolutely
positioned (or created with the Netscape 4 proprietary
<layer> tag).
<URL:http://www.mozilla.org/docs/web-deve...html#dom_acces
s> </FAQENTRY>
(better ideas obviously welcome)
In Netscape 4 elements with - position:relative - also appear in the -
layers - collection so maybe the last sentence should go "..., but only
if the element is CSS positioned (or created with ...".
Thinking about the role of FAQ entries, it seems to me that the purpose
of posting a URL reference to a part of the FAQ is to avoid repeating
the same explanation in response to questions. To that end I would
probably be a bit more long-winded and want to include an example
fallback function:-
<longer_proposal>
A common shortcut in accessing DOM elements that have ID attributes
(introduced by IE and reproduced in some other browsers) is to use the
ID string as an identifier for the element. However, not all browsers
support this shortcut and more general methods exist for accessing IDed
elements. The widest support is offered by the document.getElementById
method, which is part of the W3C DOM standard and implemented in most
modern browsers (including IE from version 5.0). So an element with
id="foo" can be referenced with:-
var el = document.getElementById("foo");
In order to support older browsers that doesn't implement this W3C DOM
method, fallback to proprietary features can be used. In Internet
Explorer 4 (and WebTV?), you can use document.all["foo"] . In Netscape
4, maybe you can use document.layers["foo"] , but only if the element is
CSS positioned (or created with the Netscape 4 proprietary <layer> tag).
E.G.:-
function getElementWithId(id){
if(document.getElementById){ //prefer the W3C DOM method.
return document.getElementById(id);
}else if(document.all){ //fallback for IE 4(and some others)
return document.all[id];
}else if(document.layers){ //fallback for Net 4 (sometimes)
return document.layers[id]; //Will not work for layers nested
//within layers. A recursive
//search of nested layers would
//be needed to find such a layer
}
} // remember to test the value returned from this function as some
// browsers do not support any of the methods used here.
</longer_proposal>
- but FAQ entries also need to be short so maybe not.
Richard.