How IDs become globals
Every DOM element that carries an id — and in some cases a name attribute — is automatically exposed as a property of the Window object, so it can be reached as window[ELEMENT_ID]. The mechanism is specified in HTML as named access on the Window object.
Take a simple element with an ID:
<div id="cool"></div>
The conventional route is to select it explicitly:
var el = querySelector("#cool");
But the ID alone is already available without either call.
Origin and compatibility
Internet Explorer shipped the behavior first, and other browsers followed. Gecko was the lone holdout in standards mode, treating it as experimental; it proceeded for the sake of browser compatibility (and even tried to persuade WebKit to keep it out of standards mode), landing in standards mode with Firefox 14.
Shadowing keeps existing globals safe
Named element references do not shadow globals that already exist. An ID matching a defined global leaves that global untouched:
<head>
<script>
window.foo = "bar";
</script>
</head>
<body>
<div id="foo">I won't override window.foo</div>
<script>
console.log(window.foo); // Prints "bar"
</script>
</body>
The reverse holds too:
<div id="foo">I will be overridden :(</div>
<script>
window.foo = "bar";
console.log(window.foo); // Prints "bar"
</script>
This precaution is what stops markup like <div id="alert" /> from knocking out the alert API — and it may be the reason the feature stays largely unnoticed.
Why the pattern is risky
Criticisms of named globals, summarized from TJ VanToll's writeup:
- References track the DOM. Change the markup and the reference changes with it — the spec itself calls this "brittle," and the HTML/JavaScript separation of concerns erodes.
- Typos become references. A slip of the finger can quietly resolve to a named global and produce unexpected results.
- Browsers disagree. An anchor such as
<a id="cool">should be reachable, but Safari and Firefox log aReferenceError. - The return value varies. With duplicate named elements in the DOM — say two
<div class="cool">— the spec says to return anHTMLCollection, while Firefox returns only the first instance. (The spec already calls for oneidper tree, though violating that doesn't break a page.) - Performance is unsettled. The browser must build and maintain the reference list; tests in this StackOverflow thread found named globals faster in one benchmark and slower in a more recent one.
Where it bites in practice
Polyfills that check for globals
Feature-detection is a standard pattern in polyfills, and that is exactly where named globals interfere. Consider a cookie set through the CookieStore API, polyfilled for browsers without it:
<body>
<img id="cookieStore"></img>
<script>
// Polyfill the CookieStore API if not yet implemented.
// https://developer.mozilla.org/en-US/docs/Web/API/CookieStore
if (!window.cookieStore) {
window.cookieStore = myCookieStorePolyfill;
}
cookieStore.set("foo", "bar");
</script>
</body>
Chrome runs this without issue. Safari throws:
TypeError: cookieStore.set is not a function
Safari does not yet support CookieStore, and the img element's ID generates a global that collides with the cookieStore global — so the polyfill never applies.
Future browser APIs
The collision can also arrive from the platform side, when a new engine feature claims a global name:
<body>
<input id="BarcodeDetector"></input>
<script>
window.BarcodeDetector.focus();
</script>
</body>
This focuses the input correctly today. Once browsers ship the BarcodeDetector API, window.BarcodeDetector will point at the API rather than the element, and .focus() will fail with "window.BarcodeDetector.focus is not a function."
The spec's own advice
Beyond the risks above, automatic globals are a reason to avoid the id attribute unless it is genuinely needed. The specification's wording is a fitting close:
As a general rule, relying on this will lead to brittle code. Which IDs end up mapping to this API can vary over time, as new features are added to the web platform, for example. Instead of this, use
document.getElementById()ordocument.querySelector().
Use querySelector or getElementById for element references; the HTML spec recommending against its own feature makes the case on its own.



