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.

Anyway.

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


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

No comments:

Post a Comment

Popular Posts