HTML id as a global variable
Learning something new every day, huh?
Today I learned that browsers add HTML elements with an
name attribute to the
<div id="hello"></div> <script> console.log(hello) </script> // Output: <div id="hello"></div>
I found a StackOverflow article from 12 years back asking about this behaviour. It turns out these global variables are named elements, while the feature is known as Named access on the window object. Internet Explorer introduced this functionality, and later, other browsers adopted it as some websites relied on the behaviour. Fascinating!
<div id="hello"></div> <script> const hello = "Something else" console.log(hello) </script> // Output: "Something else"
Live and learn!
Get in touch
I'm not currenlty looking for freelancer work, but if you want to have a chat, feel free to contact me.