Global DOM Variables
You’re probably aware that declaring a variable in the global scope (with or without var
) creates not only a global variable, but also its mirror: a property of the same name on the global
object (window
in the browser).
But what may be less common knowledge is that (because of legacy browser behavior) creating DOM elements with id
attributes creates global variables of those same names. For example:
<div id="foo"></div>
And:
if (typeof foo == "undefined") {
foo = 42; // will never run
}
console.log( foo ); // HTML element
You’re perhaps used to managing global variable tests (using typeof
or .. in window
checks) under the assumption that only JS code creates such variables, but as you can see, the contents of your hosting HTML page can also create them, which can easily throw off your existence check logic if you’re not careful.
This is yet one more reason why you should, if at all possible, avoid using global variables, and if you have to, use variables with unique names that won’t likely collide. But you also need to make sure not to collide with the HTML content as well as any other code.