Live data from Hacker News

Named element IDs can be referenced as JavaScript globals

css-tricks.com

51–60 of 113 posts

Re: Named element IDs can be referenced as JavaScript globals

#51
post #41
post #8

Another similar gotcha is that the global-scoped `name` variable must be a string. See https://developer.mozilla.org/en-US/docs/Web/API/Window/name for details. var name = true; typeof name; // "string", not "boolean" Luckily, this is not true within ES modules which you probably use most of the time anymway.

It takes a special kind of human to name variable "name" but not have it be a string.

I work with such humans! I was looking at that exact situation a few moments ago.

Re: Named element IDs can be referenced as JavaScript globals

#52
post #40

The global scope polluter has pretty bad performance and interop surprises, you shouldn't depend on it and instead use getElementById even if it's a bit more verbose. It uses a property interceptor which is fairly slow in v8: https://source.chromium.org/chromium/chromium/src/+/main:out... to call this mess of security checks: https://source.chromium.org/chromium/chromium/src/+/main:thi... which has this interop surpr…

Is there a reason to not use querySelector, since it’s a lot more flexible? One reason jQuery became so popular is because the DOM was painful to use. Things like querySelector fix that.

BTW, in my experience getElementById() is still fastest.

Re: Named element IDs can be referenced as JavaScript globals

#53
post #10
post #7

This is one of those things that pops up every year or two years. Unfortunately, the person writing about the new discovered weird trick almost always fails to precede the article with a big, red, bold "Please don't ever do this".

and then someone always follows up with "Please don't ever do this", without explaining WHY you should never do this: https://wikipedia.org/wiki/Wikipedia:Chesterton's_fence

Nice article, thanks for sharing it.

Re: Named element IDs can be referenced as JavaScript globals

#54
post #5

Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.

You shouldn't be using IDs anyways. They are just bad for a lot of reasons. You can only have one on a page and it reduces your reusability. Use classes instead.

ID's aren't bad, they're unique identifiers, and useful for (deep) linking to specific pieces of content within documents. Please use ID's as liberally as you please, and use them for their proper use.

Re: Named element IDs can be referenced as JavaScript globals

#55
post #21

I don't want to sound like I have an axe to grind (but I do), but this is the kind of feature/wart that shows the age of the HTML/CSS/JS stack. The whole thing is ripe for a redo. I know they get a lot of hate, but of all the big players in this space I think FB is the best equipped to do this in a way that doesn't ruin everything. I just wonder if they have an incentive (maybe trying to break the Google/MS hegemony…

There’s always WASM, and I think Zuck is more interested in VR than trying to push a new web standard.

Re: Named element IDs can be referenced as JavaScript globals

#56
post #9
post #5

Now 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

And then get coworkers to remove it because they don’t understand that you can create scopes like that

Re: Named element IDs can be referenced as JavaScript globals

#57
post #2

It hurts

I think it would hurt less with TypeScript global types. Just need to know what IDs you'd expect to find in the DOM.

the ID's in DOM will never conflict or cause an issue with your own JS code. You can't reliably use 'named access on the window object' (the name of this feature) because of this, so it's never a problem, and also largely useless.

Re: Named element IDs can be referenced as JavaScript globals

#58
post #21

I don't want to sound like I have an axe to grind (but I do), but this is the kind of feature/wart that shows the age of the HTML/CSS/JS stack. The whole thing is ripe for a redo. I know they get a lot of hate, but of all the big players in this space I think FB is the best equipped to do this in a way that doesn't ruin everything. I just wonder if they have an incentive (maybe trying to break the Google/MS hegemony…

I find it pretty funny that we humans have invented all these transpilers and bundlers, invested probably billions of dollars in JITs, just to keep writing JS

Re: Named element IDs can be referenced as JavaScript globals

#59
post #5

Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.

You shouldn't be using IDs anyways. They are just bad for a lot of reasons. You can only have one on a page and it reduces your reusability. Use classes instead.

Use ids when JS needs to reference unique elements. Use classes for styling and accessing groups.

Re: Named element IDs can be referenced as JavaScript globals

#60

Earlier quoted context omitted.

Is there a reason to not use querySelector, since it’s a lot more flexible? One reason jQuery became so popular is because the DOM was painful to use. Things like querySelector fix that.

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.
Post reply on HN