Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My IE doesn't support "document.getElementById()"!

My IE is IE 6. It's pretty old, but I have to use it.

I just found a strange problem, it doesn't support "document.getElementById()"!

See my test file: test.html

<a id="aaa">xxx</a>
<script>
aaa = document.getElementById("aaa");
alert(aaa);
</script>

When I open this file by IE, there shows an ERROR dialog:

line: 3
char: 1
error: object doesn't support the attribute or method
code: 0
URL: file://D:/test.html

Do I made some mistakes? It's so strange ~

like image 427
Freewind Avatar asked Jul 08 '10 14:07

Freewind


2 Answers

Its because the anchor element is set up (in IE6) as a global variable with name aaa. And then you are trying to use another variable with same name.

If you change it to...

<a id="aaa">xxx</a>
<script>
bbb = document.getElementById("aaa");
alert(bbb);
</script>

it should work.

See http://verens.com/2005/03/18/getelementbyid-bug-in-ie6/

like image 69
barrylloyd Avatar answered Sep 18 '22 05:09

barrylloyd


Change the variable name so that its not the same as the element id.

like image 44
heisenberg Avatar answered Sep 20 '22 05:09

heisenberg