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 thi开发者_如何学JAVAs 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 ~
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/
Change the variable name so that its not the same as the element id.
As noted by barrylloyd, its because the anchor element is set up (in IE6) as a global variable with name aaa. You can use var to create a local variable called aaa:
<a id="aaa">xxx</a>
<script type="text/javascript">
var aaa = document.getElementById("aaa");
alert(aaa);
</script>
Is this a snippet of your html file or the entire file? In the first case I'd suggest adding the appropriate tags (<html>
, <body>
), and a doctype. Second, the element should be loaded when you execute this javascript, but with IE6, I would not rely on it. Therefore you might want to try it inside an onload function:
window.onLoad = function() {
alert(document.getElementById("aaa"));
}
It works if you place the javascript block within the <head>
tag section. There's where JS should normally be placed anyway.
精彩评论