2 ms·
TIL that elements with ids are exposed as global variables in JS.
by naniwaduni 3y ago
TIL that elements with ids are exposed as global variables in JS.
- onetom 3y agoI'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?
- serial_dev 3y agoHTML 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#named-access-on-the-window-object https://html.spec.whatwg.org/multipage/nav-history-apis.html...
- lifthrasiir 3y agoThe relevant WHATWG specification: https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-window-nameditem 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-as-javascript-variable https://stackoverflow.com/questions/9740275/html-element-id-...