Possible Duplicate:
IE/Chrome: are DOM tree elements global variables here?
I just stumbled over a weird issue (a combination of Stack Overflow JavaScript and a user script someone was running) that I could trace back to the fact that the user script was was accessing a variable that no longer existed (because we made some changes in the SO JS).
But instead of being undefined
/ throwing errors, this variable was actually returning a DOM element, which happened to have the variable's name as its element ID.
I was sure this was a Chrome bug and was just about to file a report, so I tested other browsers, only to notice they exhibit similar – but not identical – behavior.
Test case:
<body id="a-b-c">
<div id="something"></div>
something has type <script>document.write(typeof something)</script><br>
window["a-b-c"] has type
<script>document.write(typeof window["a-b-c"])</script><br>
window.hasOwnProperty("something") is
<script>document.write(window.hasOwnProperty("something"))</script><br>
something === document.getElementById("something") is
<script>document.write(something === document.getElementById("something"))</script><br>
</body>
Outputs:
Firefox:
something has type object
window["a-b-c"] has type undefined
window.hasOwnProperty("something") is true
something === document.getElementById("something") is true
IE8:
something has type object
window["a-b-c"] has type object
window.hasOwnProperty("something") is
something === document.getElementById("something") is true
Chrome:
something has type object
window["a-b-c"] has type object
window.hasOwnProperty("something") is false
something === document.getElementById("something") is true
Safari/Opera:
something has type object
window["a-b-c"] has type object
window.hasOwnProperty("something") is true
something === document.getElementById("something") is true
So my question:
- Is this behavior defined anywhere?
- If so, which of the browsers is doing it right?
- Is there a situation where this is actually helpful? I can only come up with problems this creates, but really none that it solves.