Live data from Hacker News

Named element IDs can be referenced as JavaScript globals

css-tricks.com

21–30 of 113 posts

Re: Named element IDs can be referenced as JavaScript globals

#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 on search?).

Re: Named element IDs can be referenced as JavaScript globals

#22
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…

The best equipped to do this are Google/MS/Apple because they actually control the source code of relevant contemporary browsers.

Re: Named element IDs can be referenced as JavaScript globals

#23
post #22
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…

The best equipped to do this are Google/MS/Apple because they actually control the source code of relevant contemporary browsers.

I think that this is the case (right now) because of Apple's stranglehold on the browser on iOS and the complex relationship between Google/Apple.

If FB could launch a browser on iOS that was in their walled garden, not only would it quickly receive wide adoption but it might become people's primary browser.

Not that I necessarily think that's a good thing, mind you.

Re: Named element IDs can be referenced as JavaScript globals

#24
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.

If you read the article and the spec, you'll see that any explicitly created variables will always take precedence over automatic IDs, so any globals will always override these IDs.

In the additional considerations section [1], they mention about not consistent behaviors between browsers. Those are the kind of issues that are quite difficult to debug.

[1] https://css-tricks.com/named-element-ids-can-be-referenced-a...

Re: Named element IDs can be referenced as JavaScript globals

#25
post #23
post #22

Earlier quoted context omitted.

The best equipped to do this are Google/MS/Apple because they actually control the source code of relevant contemporary browsers.

I think that this is the case (right now) because of Apple's stranglehold on the browser on iOS and the complex relationship between Google/Apple. If FB could launch a browser on iOS that was in their walled garden, not only would it quickly receive wide adoption but it might become people's primary browser. Not that I necessarily think that's a good thing, mind you.

Why would it quickly receive any adoption? Of all of the behemoths, I would trust FB the least here. Not that I trust any of the other big players enough not to use Firefox everywhere I can.

Re: Named element IDs can be referenced as JavaScript globals

#26
post #20

I saw this "shortcut" used in code snippets, on online JS/CSS/HTML editors like JSFiddle. It did not even occur to me this was part of JS spec, I thought the editor was generating code behind my back!

> It did not even occur to me this was part of JS spec,

It has nothing to do with JS spec; it's part of the DOM as defined by the HTML spec.

Re: Named element IDs can be referenced as JavaScript globals

#27
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(this); // Window
            console.log(globalThis); // Window
            console.log("script", cool.innerHTML); // script cool
        
        
            console.log(this); // undefined
            console.log(globalThis); // Window
            console.log("module", cool.innerHTML); // module cool
        
    
This seems like a missed opportunity. JavaScript Modules should have been required to "import {window} from 'dom'" or something, clearing out its global namespace.

Re: Named element IDs can be referenced as JavaScript globals

#28
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.

I've been doing JS for like fifteen years, this one I never knew. Wow.

I must have never used "name" as a name for a global variable or just for ones that were strings.

Re: Named element IDs can be referenced as JavaScript globals

#29
post #25
post #23

Earlier quoted context omitted.

I think that this is the case (right now) because of Apple's stranglehold on the browser on iOS and the complex relationship between Google/Apple. If FB could launch a browser on iOS that was in their walled garden, not only would it quickly receive wide adoption but it might become people's primary browser. Not that I necessarily think that's a good thing, mind you.

Why would it quickly receive any adoption? Of all of the behemoths, I would trust FB the least here. Not that I trust any of the other big players enough not to use Firefox everywhere I can.

The word "trust" doesn't factor into ~90% of users' decisions.

If FB says "hey install this app," they will install it.

Re: Named element IDs can be referenced as JavaScript globals

#30

Earlier quoted context omitted.

It's has been explained enough times. It's just that looking things up for yourself seems to have gone out of fashion.

That doesn’t help people who stumble upon this when searching for the problem. All the “look it up” response does is make sure the search results are a bunch of content saying “look it up”, which isn’t really that helpful.

It is explained fairly early in the article.

This used to be done quite a lot in the early JS days when scope was kind of thrown out the window (no pun) and you just did whatever dirty thing you needed to in order to make a page work.

Post reply on HN