4 ms·
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
by kiawe_fire 4y ago
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.
- dphnx 4y ago`document.all` can be used in this way: <div id="foo"></div> <script> const { foo } = document.all // do something with foo </script> Don't use it though, it's deprecated as well[1]. [1]: https://developer.mozilla.org/en-US/docs/Web/API/Document/all https://developer.mozilla.org/en-US/docs/Web/API/Document/al...
- bobince 4y agoThe 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
- WorldMaker 4y agoAs the article points out, this initiative was an 90s IE one and the Gecko team (Firefox, post-Netscape) were against it.
- akira2501 4y agoYou can make this yourself with Proxy. I get a lot of mileage out of this: // 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 <div id="something></div> You can just do $id.something.innerHTML = 'inside!';