views:

128

answers:

5

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 ~

A: 

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"));
}
Peter Kruithof
All good advice, but not actually the problem.
Kramii
@Peter, it's the entire file. I add tags as you said, but the problem is still there. And, @barrylloyd is correct. But thank you all the same
Freewind
+7  A: 

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/

barrylloyd
I don't observe any difference in behavior between his code and yours using IE6 on XP.
Lord Torgamus
@Torgamus - did you actually read his explanation?
meder
@meder, of course I did. I ran both sets of code locally, and there's no behavior difference on my machine. If it's just my machine, then fine. And from the upvotes I gather that it is. I wasn't accusing him of being wrong, just stating that fact; no need to accuse me of not reading.
Lord Torgamus
Can you link to your test?
meder
@barrylloyd, thank you!! Yes, you are right~~ If you don't tell me this, I will never know. I must spend several hours or days to debug this problem. I love stackoverflow, and love you all!
Freewind
+3  A: 

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

kekekela
@kekekela, thank you, you are right too
Freewind
A: 

It works if you place the javascript block within the <head> tag section. There's where JS should normally be placed anyway.

Moses Ting
That's not true, and performance-wise not recommended: http://developer.yahoo.net/blog/archives/2007/07/high_performanc_5.html
Peter Kruithof
@Moses, thank you all the same :)
Freewind
Hm, interesting point about moving scripts to the bottom to speed up rendering.
Moses Ting
+4  A: 

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>
Kramii
You and barry have got to the answer but the terminology needs a little finessing. The variable you create above __is__ a global variable not a local variable. What IE does is a little trick that if a search up the scope chain for an identifier fails even at the global level __then__ it searches its index of element ids, if it finds a match it returns it. This is not something other engines do and its really annoying the IE does it.
AnthonyWJones
@Kramii, thank you :)
Freewind
@AnthonyWJones, great clarification. Thanks!
Kramii