I'm surprised to find that this trick still works even in the new backwards-incompatible JavaScript Modules (using ), which enables "strict" mode and a number of other strictness improvements by default. I believe it works because the global object ("globalThis") is the Window in either case; this is why JavaScript Modules can refer to "window" in the global scope without explicitly importing it. cool console.log(thi…
There is some effort to standardize something along these lines. Well, some things which combined would achieve this. It’s too late to bake it into ESM, but I believe it’ll be possible with ShadowRealms[1] and/or SES[2], and Built-in Modules (JS STL)[3]. 1: https://github.com/tc39/proposal-shadowrealm 2: https://github.com/tc39/proposal-ses 3: https://github.com/tc39/proposal-built-in-modules
Named element IDs can be referenced as JavaScript globals
91–100 of 113 posts
Re: Named element IDs can be referenced as JavaScript globals
#92Earlier quoted context omitted.
Use ids when JS needs to reference unique elements. Use classes for styling and accessing groups.
JS can do just as well with unique classnames, which avoids issues with ids like those given in the article.
Re: Named element IDs can be referenced as JavaScript globals
#93Earlier quoted context omitted.
BTW, in my experience getElementById() is still fastest.
That’s surprising, webkit+blink and I’m guessing gecko all optimize the query selector cases. I assume it’s the cost of the NodeList (because NodeLists are live :-/)
Re: Named element IDs can be referenced as JavaScript globals
#94I wouldn't use it in production, but it's handy for banging together a proof-of-concept.
Re: Named element IDs can be referenced as JavaScript globals
#95Earlier quoted context omitted.
BTW, in my experience getElementById() is still fastest.
In isolation definitely, but in real world code it might be faster to use querySelector for branchy code if it doesn’t always use an id. As with everything, if it’s not performance-sensitive write the code that’s easier for humans to read, and if it is measure first.
Re: Named element IDs can be referenced as JavaScript globals
#96I don't think this article is complete. It mentions no pollution, which is true of window and most HTML elements, but not always. Check this out, you can set an img name to getElementById and now document.getElementById is the image element! Here's a minimal example ( https://jsfiddle.net/wc5dn9x2/ ): // The img object console.log(document.getElementById); // TypeError: document.getElementById is not a function :D co…
It's weirdly not that discussed on the web, most probably because it require a pretty specific situation.
Re: Named element IDs can be referenced as JavaScript globals
#97Earlier quoted context omitted.
That’s surprising, webkit+blink and I’m guessing gecko all optimize the query selector cases. I assume it’s the cost of the NodeList (because NodeLists are live :-/)
May be worth testing it against getElementsByClassName(), which also returns a live collection.
Many many years ago I recall querySelector starting out with a check for #someCSSIdentifier and shortcutting to the getElementById path, but maybe my memory is playing tricks on me.
Re: Named element IDs can be referenced as JavaScript globals
#98Earlier quoted context omitted.
The performance difference is negligible. Both methods can return 70k-100k selections in 10ms.
I had projects where this contributed to a visibly perceivable difference. This may have involved SVG, though.
Re: Named element IDs can be referenced as JavaScript globals
#99Earlier quoted context omitted.
> Is there a reason to not use querySelector getElement is slightly faster, but not by enough to care IIRC so I use querySelector for consistency and it's flexibility. > One reason jQuery became so popular is because the DOM was painful I would say that is the key reason, with everything else being collateral benefits. Assuming you combine element selection, dealing with legacy incompatibilities, and function chainin…
...and portability.
I was counting that in "dealing with legacy incompatibilities".
Re: Named element IDs can be referenced as JavaScript globals
#100I don't think this article is complete. It mentions no pollution, which is true of window and most HTML elements, but not always. Check this out, you can set an img name to getElementById and now document.getElementById is the image element! Here's a minimal example ( https://jsfiddle.net/wc5dn9x2/ ): // The img object console.log(document.getElementById); // TypeError: document.getElementById is not a function :D co…
Note that this is with the name attribute, not the id attribute the article is discussing.