This method will return an error if the ID attribute provided to the method is identical to the name of a variable or a function being used in the javascript code.
This will generate the message "Object doesn't support this property or method"
<SCRIPT>
function fnGetId(){ // Returns the first DIV element in the collection.
var oDiv1=document.getElementById("oDiv1");}
</SCRIPT>
<DIV ID="oDiv1">Div #1</DIV>
<DIV ID="oDiv2">Div #2</DIV>
<DIV ID="oDiv3">Div #3</DIV>
<INPUT TYPE="button" VALUE="Get Names" onclick="fnGetId()">
[jsudds] This isn't a "working" example of the problem. The script here, which was copied from the topic above, does not generate an error message. I tried to implement the behavior you describe, by using the script name and a global object, but both IE6 and IE7 run it flawlessly.
<script type="text/javascript">
function fnGetId()
{
var oDiv1 = document.getElementById("fnGetId");
alert(oDiv1.innerHTML);
var oDiv2 = document.getElementById("window");
alert(oDiv2.innerHTML);
}
</script>
<div id="fnGetId">Div #1</div>
<div id="window">Div #2</div>
<input type="button" value="Get Names" onclick="fnGetId()"/>Can you provide a repro?
[sglass] There's no doubt this exists. We've been dealing with this issue for the past two days. Turns out we were using this shared library called mootools. Within mootools they had defined a function called getElementById(). Once I renamed this method in mootools, my problem dissappeared. This seems to be plaguing a lot of IE users out there. Hopefully they'll find this and get past it.
[arnoschaefer]
I can reproduce the "Object doesn't support this property or method" error in IE8 when omitting the "var" keyword, thus declaring the variable implicitly in the global context:
<script type="text/javascript">
function doit () {
a = document.getElementById ("a");
alert (a);
}
</script>
</head>
<body onload="doit ();">
<h1 id="a">foo</h1>
</body>
It appears that IE implicitly declares named DOM nodes as "constants" in the global scope, so I can access "a" directly without using "getElementById" first (try commenting out the "getElementById" line). Every attempt to overwrite these generates the abovementioned error. The error can of course be fixed by using the "var" keyword, thus putting the variable "a" in the local scope. Does anyone know more about this behavior? Is this documented elsewhere?