CSS ID's Create Javascript Globals

Wow. I learned something interesting the other day. Namely, that css ID's become part of the javascript global namespace. I'm not quite sure how I feel about this (not that it matters how I feel about it). I had never really thought about where it stored the relevant information. But it seems a little weird. And I imagine this could result in some head-scratcher bugs if you used an ID that overwrites something you are depending on. However, I don't remember ever being bitten by this.


Here's a snippet that you can use to confirm for yourself (jsfiddle for this is at http://jsfiddle.net/90rwhoy6/10/):

      <div id="heyThere">This is something</div>
        alert(heyThere); //it's in the global namespace(!!)
Boy - it seems like you could really cause odd problems
for yourself with carelessly defined ID's

Popular Posts