News for hackers. Stuff that matters.
Re:Named element IDs can be referenced as JavaScript globals by kiawe_fire on Tuesday September 27, 2022 @12:09PM (#32997270)
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.

Re:Named element IDs can be referenced as JavaScript globals by dphnx on Tuesday September 27, 2022 @12:27PM (#32997556)
`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/al...

Re:Named element IDs can be referenced as JavaScript globals by akira2501 on Tuesday September 27, 2022 @01:40PM (#32998531)
You 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!';
Re:Named element IDs can be referenced as JavaScript globals by bobince on Tuesday September 27, 2022 @12:41PM (#32997755)
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

Re:Named element IDs can be referenced as JavaScript globals by WorldMaker on Tuesday September 27, 2022 @01:34PM (#32998454)
As the article points out, this initiative was an 90s IE one and the Gecko team (Firefox, post-Netscape) were against it.