Live data from Hacker News

Named element IDs can be referenced as JavaScript globals

css-tricks.com

81–90 of 113 posts

Re: Named element IDs can be referenced as JavaScript globals

#82
This always reminded me of PHP’s infamous register_globals. For those unfamiliar, anything in the ‘$_REQUEST’ array (which itself comprises of $_POST, $_GET, and $_COOKIE merged together) is added to the global scope. So if you made a request to index.php?username=root, $username would contain “root” unless it was explicitly initialized it before it was used.

Re: Named element IDs can be referenced as JavaScript globals

#83

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.

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

#84
post #6

This 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.

Yep, same here. The only time I use this bit of knowledge nowadays is in the console. If I see a tag has an ID, I save myself a few characters by just referring to it as a variable since I know it's already there anyways.

IDs were the only way to get a reference to an element early on if I'm remembering correctly. Or maybe the DOM API just wasn't well known. All the examples and docs just used IDs, that I can remember for sure.

Re: Named element IDs can be referenced as JavaScript globals

#86

I 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.

Re: Named element IDs can be referenced as JavaScript globals

#87

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.

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

Re: Named element IDs can be referenced as JavaScript globals

#88

Earlier quoted context omitted.

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.

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

#89
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".

I discovered that with a couple of friends while in JavaScript class. Every one of us was like "this is actually horrible".

Re: Named element IDs can be referenced as JavaScript globals

#90
For me, the disadvantage above any listed on the blog is that if I saw this global variable referenced in some code (especially old code, where some parts might be defunct), I would have absolutely no idea where it came from, and I bet a lot of others would struggle too.
Post reply on HN