4 ms·
Are DOM tree elements global variables?
- khuey 15y agoGecko (Firefox) does this in quirks mode only. We're holding the line against this for the moment (http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960 http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960).
- pak 15y agotldr version, minus the usual stackoverflow backandforth: 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. With some quick testing in Chrome, IE, and Opera, it appears that these elements are enumerable off of "window" in Opera but not Chrome and IE (perhaps those two browsers create them only when you access them).
- snorkel 15y agoYes.
- mrspeaker 15y agoYep, 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 ;)
- ootachi 15y agoThen you'd be breaking the rules of JS1K, because JS1K demos have to work in Firefox, and Firefox doesn't support this feature (except in quirks mode, which you can't turn on in JS1K).
- crizCraig 15y agoThe IE guys are saying this needs to be kept for backwards compatibility. http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960#c8 http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960#c8 Why can't it be kept for old doctypes and quirksmode like Gecko does? The HTML5 doctype should get rid of this. Surely that wouldn't break top sites as MSFT is saying: http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960#c12 http://www.w3.org/Bugs/Public/show_bug.cgi?id=11960#c12
- ch0wn 15y agoYou can, however, abuse this quite nicely for size-optimized javascript demos. For example if you look at some of the http://js1k.com/ http://js1k.com/ submissions. For anything other than those experiments, you should of course avoid.