2

Javascript on many (all?) modern browsers seems to create variables on the window object for DOM elements with IDs.

Example: http://jsfiddle.net/LXmG4/

<div id="a_js_var_will_be_created_automatically_for_this">See the console</div>
<script>console.log(a_js_var_will_be_created_automatically_for_this);</script>

I read a few posts from 2011 that state that this feature/bug/quirk is expected to be removed, yet I see this work in modern Chrome, Firefox, Safari, IE.

What's the present time data on this?

  • Just dont rely on that stuff,that's bad practice.Yes it still works,it's just not good DOM programming. – mpm May 15 '14 at 02:54
  • yeah it works, and works everywhere, but don't use it? makes sense if you don't think about it. one could point out that it's last in the scope chain, not that that usually matters, or that adding another element with the same id will turn the global into a collection. i've started using this a lot as uname.value is easier to read and code than document.getElementById("uname").value or even $("#uname").val(). the shift-key reduction itself makes it worth it, and for static elms, it's easy enough to loop [id]s and create globals for older browsers... – dandavis May 15 '14 at 03:38

0 Answers0