3

html code:

<div id="register"></div>

Javascript code:

console.log( register );//without even declaring the variable register

it returns <div id=​"register"></div> in the console.

Is that a normal behavior? (tested in Chrome and Firefox)

Is it documented somewhere?

Is it part of the ECMAScript specification?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Jad Joubran
  • 2,511
  • 3
  • 31
  • 57
  • It's normal behavior. – Pointy May 19 '14 at 14:48
  • 1
    Wow, interesting. I just tested IE11, and it does the same; I was unaware of this. – Katana314 May 19 '14 at 14:50
  • Microsoft [wrote](http://blogs.msdn.com/b/alvar/archive/2009/10/22/internet-explorer-creates-global-variables-for-each-object-in-the-dom.aspx) of this back in 2009 ("The cause for this behaviour is that Internet Explorer creates global variables for each element of the DOM.") – j08691 May 19 '14 at 14:51

1 Answers1

-3

This is DOM level 1, but doesn't wort in old IEs, IIRC.

airportyh
  • 21,948
  • 13
  • 58
  • 72
  • 1
    Old versions of IE do it too. There are dozens of old questions about "why does my JavaScript work in IE but not Firefox" caused by this because old versions of *Firefox* didn't do it. – Pointy May 19 '14 at 14:51
  • 1
    Where in DOM level 1 is this described? You've linked to the spec's homepage. The spec is rather large. – Quentin May 19 '14 at 14:52