Named element IDs can be referenced as JavaScript globals
1–10 of 113 posts
Re: Named element IDs can be referenced as JavaScript globals
#2It hurts
Re: Named element IDs can be referenced as JavaScript globals
#3It 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.
Re: Named element IDs can be referenced as JavaScript globals
#4I discovered this the hard way and I am still really torn. The entire window global object is just a minefield.
Re: Named element IDs can be referenced as JavaScript globals
#5Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.
Re: Named element IDs can be referenced as JavaScript globals
#6This has been a thing since the 90s. I really wish we'd done away with it for any document that specifies itself as HTML5.
It's great for hacking a tiny script together, however.
Re: Named element IDs can be referenced as JavaScript globals
#7This 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".
Re: Named element IDs can be referenced as JavaScript globals
#8Another 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.Re: Named element IDs can be referenced as JavaScript globals
#9Now 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.
Re: Named element IDs can be referenced as JavaScript globals
#10This 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: