Seems like something that could have been made safer just by name spacing it a bit better. Something like “window.elements.myDiv”? I wonder why the decision to go straight to the root.
The Netscape of the 90s wasn't interested in making features ‘safe’. They were about throwing out features as quickly as possible to see what would stick. The simplest possible syntax is to make named elements available globally, and if that clashes with future additions to the DOM API then well that's a problem for some future idiots to worry about. as a strategy it worked pretty well, unfortunately
Named element IDs can be referenced as JavaScript globals
71–80 of 113 posts
Re: Named element IDs can be referenced as JavaScript globals
#72Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.
Avoid globals at all costs - use IIFE [1] instead, wrapping your function in parenthesis and invoking it right away. [1] https://developer.mozilla.org/en-US/docs/Glossary/IIFE
Re: Named element IDs can be referenced as JavaScript globals
#73Seems like something that could have been made safer just by name spacing it a bit better. Something like “window.elements.myDiv”? I wonder why the decision to go straight to the root.
// proxy to simplify loading and caching of getElementById calls
const $id = new Proxy({}, {
// get element from cache, or from DOM
get: (tgt, k, r) => (tgt[k] || ((r = document.getElementById(k)) && (tgt[k] = r))),
// prevent overwriting
set: () => $throw(`Attempt to overwrite id cache key!`)
});
Now if you have
You can just do $id.something.innerHTML = 'inside!';Re: Named element IDs can be referenced as JavaScript globals
#74This doesn't seem to be true as shown within this fiddle: https://jsfiddle.net/L785cpdo/1/
Bear in mind that only undefined elements will be declared this way
Re: Named element IDs can be referenced as JavaScript globals
#75Re: Named element IDs can be referenced as JavaScript globals
#76*rigamorale Should read “rigamarole”
Re: Named element IDs can be referenced as JavaScript globals
#77*rigamorale Should read “rigamarole”
Today I learned, it's both!
Re: Named element IDs can be referenced as JavaScript globals
#78Earlier quoted context omitted.
BTW, in my experience getElementById() is still fastest.
The performance difference is negligible. Both methods can return 70k-100k selections in 10ms.
Re: Named element IDs can be referenced as JavaScript globals
#79>To add insult to the injury, named elements are accessible as global variables only if the names contain nothing but letter. This doesn't seem to be true as shown within this fiddle: https://jsfiddle.net/L785cpdo/1/ Bear in mind that only undefined elements will be declared this way
Re: Named element IDs can be referenced as JavaScript globals
#80Earlier quoted context omitted.
BTW, in my experience getElementById() is still fastest.
The performance difference is negligible. Both methods can return 70k-100k selections in 10ms.
Also there's a performance cliff when you have a lot of unique ids (or selectors in use from JS).
When you hit the cache querySelector is primarily a getElementById call and then some overhead to match the selector a second time (which chrome should really optimize):
https://source.chromium.org/chromium/chromium/src/+/main:thi...
But if you have more than 256 selectors and ids in use:
https://source.chromium.org/chromium/chromium/src/+/main:thi...
You'll start to hit the selector parser a lot more and then querySelector will be a fair bit slower going through the CSS parser.