https://css-tricks.com/named-element-ids-can-be-referenced-as-javascript-globals/ Skip to main content CSS-Tricks * Articles * Videos * Almanac * Newsletter * Guides * DigitalOcean * DO Community Search global scope JavaScript Named Element IDs Can Be Referenced as JavaScript Globals Avatar of Matteo MazzaroloAvatar of Matteo Mazzarolo Matteo Mazzarolo on Sep 27, 2022 DigitalOcean joining forces with CSS-Tricks! Special welcome offer: get $200 of free credit. Did you know that DOM elements with IDs are accessible in JavaScript as global variables? It's one of those things that's been around, like, forever but I'm really digging into it for the first time. If this is the first time you're hearing about it, brace yourself! We can see it in action simply by adding an ID to an element in HTML:
Normally, we'd define a new variable using querySelector("#cool") or getElementById("cool") to select that element: var el = querySelector("#cool"); But we actually already have access to #cool without that rigamorale: CodePen Embed Fallback So, any id -- or name attribute, for that matter -- in the HTML can be accessed in JavaScript using window[ELEMENT_ID]. Again, this isn't exactly "new" but it's really uncommon to see. As you may guess, accessing the global scope with named references isn't the greatest idea. Some folks have come to call this the "global scope polluter." We'll get into why that is, but first... Some context This approach is outlined in the HTML specification, where it's described as "named access on the Window object." Internet Explorer was the first to implement the feature. All other browsers added it as well. Gecko was the only browser at the time to not support it directly in standards mode, opting instead to make it an experimental feature. There was hesitation to implement it at all, but it moved ahead in the name of browser compatibility (Gecko even tried to convince WebKit to move it out of standards mode) and eventually made it to standards mode in Firefox 14. One thing that might not be well known is that browsers had to put in place a few precautionary measures -- with varying degrees of success -- to ensure generated globals don't break the webpage. One such measure is... Variable shadowing Probably the most interesting part of this feature is that named element references don't shadow existing global variables. So, if a DOM element has an id that is already defined as a global, it won't override the existing one. For example:and try it.) This and a few other things in the article suggest that the author hasn't understood how this whole thing is actually implemented (which is admittedly not awfully clear from the spec if you don't understand JavaScript's prototypal inheritance model or the nature of the window global object). Without getting into the details: this isn't the browser setting properties when you provide an element with such-and-such an ID, but rather the fallback, so that when you try accessing a property that doesn't exist on the global object, it will try finding a suitable object to give you, instead of giving you undefined straight away. Reply + Matteo Permalink to comment# September 27, 2022 Yeah, that bonus point shouldn't have slipped in -- my bad. Thanks for reporting! I'll also double-check what you pointed out in the other comment. 5. Manuel Strehl Permalink to comment# September 27, 2022 Additional "fun fact": The same mechanism is available on the HTMLFormElement interface, i.e. on all
Reply 6. Reed Permalink to comment# September 27, 2022 I wonder what was actually happening in your case, because according to the blog post the value of window.config should be the inline-object. Reply Leave a Reply Cancel reply Your email address will not be published. Required fields are marked * [ ] [ ] [ ] [ ] [ ] [ ] [ ] Comment * [ ] Name * [ ] Email * [ ] Website [ ] [ ] Save my name, email, and website in this browser for the next time I comment. [ ]Get the CSS-Tricks newsletter [Post Comment] Copy and paste this code: micuno * [4.1 ] Leave this field empty [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] D[ ] CSS-Tricks is powered by DigitalOcean. Keep up to date on web dev with our hand-crafted newsletter DigitalOcean * DigitalOcean * DigitalOcean Community * About DigitalOcean * Legal * Free Credit Offer CSS-Tricks * Email * Guest Writing * Book * Advertising Follow * Twitter * Instagram * YouTube * CodePen * iTunes * RSS Back to Top [Jetpack-Search-on-CSS-Tricks-Instant-Search]