2

Possible Duplicate:
IE/Chrome: are DOM tree elements global variables here?

If I have a HTML page containing

<div id='myDiv'></div>

I would usually access this element like so

var myDiv = document.getElementById('myDiv');
myDiv.innerHTML = '';

However in Chrome (v20) I can also access it using

myDiv.innerHTML = '';

or

window['myDiv'].innerHTML = '';

I've tested in Firefox(v13) and IE(v9) and neither support this.

Why does Chrome support this and when was this added? Are there any implications? (I'm assuming that if any variables are declared with the same name then the name will just point at the latest declaration).

I stumbled on this because I was unintentionally using window['id'] to access an element and it all worked until I tried another browser.

Community
  • 1
  • 1
Chris Herring
  • 3,675
  • 3
  • 33
  • 50
  • I can see this being convenient and improving performance by saving the need to look up the element however I'm curious if this is part of any ECMAScript standard or Google has just decided it's a nice feature. – Chris Herring Jul 13 '12 at 06:43
  • Related: http://stackoverflow.com/questions/3434278/ie-chrome-are-dom-tree-elements-global-variables-here – stewe Jul 13 '12 at 06:51
  • @stewe Ah I searched for an existing question but couldn't find one. Cheers. – Chris Herring Jul 13 '12 at 06:55

0 Answers0