Hacker News new | ask | show | jobs
by naniwaduni 857 days ago
TIL that elements with ids are exposed as global variables in JS.
1 comments

I've also noticed this a few years ago, but where is this documented? Was it already the case in IE6 for example? Have you found any definitive source, which states this behaviour?
The relevant WHATWG specification: https://html.spec.whatwg.org/multipage/nav-history-apis.html...

It was said to be introduced in MSIE 4 or around and then eventually standardized by WHATWG (because `window` was never formally specified before that point, AFAIK): https://stackoverflow.com/questions/9740275/html-element-id-...

HTML Spec:

> 7.2.2.3 Named access on the Window object

> window[name]

> Returns the indicated element or collection of elements.

https://html.spec.whatwg.org/multipage/nav-history-apis.html...