2

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.
Community
  • 1
  • 1
balpha
  • 50,022
  • 18
  • 110
  • 131
  • 2
    bobince has provided a good analysis of this issue when answering this question: http://stackoverflow.com/questions/3434278/ie-chrome-are-dom-tree-elements-global-variables-here – meouw Mar 16 '11 at 21:08
  • Very strange... why should window.something exist at all? Is the user script adding a property to the window object when the DOM element is created? – glomad Mar 16 '11 at 21:14
  • meouw's comment should be reposted as an answer :) – glomad Mar 16 '11 at 21:14
  • Weird ... http://jsfiddle.net/simevidas/Qu5zv/ – Šime Vidas Mar 16 '11 at 21:18
  • @ithcy: No, the user script was trying to access a variable that previously existed (put into the global namespace by the SO JavaScript). Since I removed that variable today, the user script instead received the dom element with that id. – balpha Mar 16 '11 at 21:21

0 Answers0