TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Are DOM tree elements global variables?

48 pointsby bruthalmost 14 years ago

6 comments

khueyalmost 14 years ago
Gecko (Firefox) does this in quirks mode only. We're holding the line against this for the moment (<a href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960" rel="nofollow">http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960</a>).
pakalmost 14 years ago
tldr version, minus the usual stackoverflow backandforth:<p>Named elements are made into properties of the "window" object in Chrome, IE, and Opera. That would include any element with an "id" attribute and most elements with a "name" attribute. Thankfully, any vars you declare in this scope override the DOM ones.<p>With some quick testing in Chrome, IE, and Opera, it appears that these elements are enumerable off of "window" in Opera but <i>not</i> Chrome and IE (perhaps those two browsers create them only when you access them).
mrspeakeralmost 14 years ago
Yep, Microsoft did it, and everyone else (for some reason) followed. My approach is to actively avoid relying on it for real work - but pull it out for the JS1K/10K comps ;)
评论 #2896151 未加载
ch0wnalmost 14 years ago
You can, however, abuse this quite nicely for size-optimized javascript demos. For example if you look at some of the <a href="http://js1k.com/" rel="nofollow">http://js1k.com/</a> submissions. For anything other than those experiments, you should of course avoid.
crizCraigalmost 14 years ago
The IE guys are saying this needs to be kept for backwards compatibility. <a href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960#c8" rel="nofollow">http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960#c8</a><p>Why can't it be kept for old doctypes and quirksmode like Gecko does? The HTML5 doctype should get rid of this. Surely <i>that</i> wouldn't break top sites as MSFT is saying: <a href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960#c12" rel="nofollow">http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960#c12</a>
snorkelalmost 14 years ago
Yes.