446

Working on an idea for a simple HTMLElement wrapper I stumbled upon the following for Internet Explorer and Chrome:

For a given HTMLElement with an id in the DOM tree, it is possible to retrieve the <div> using its ID as a variable name or as a property of window. So for a <div> like

<div id="example">some text</div>

in Internet Explorer 8 and Chrome you can do:

alert(example.innerHTML); // Alerts "some text".

or

alert(window["example"].innerHTML); // Alerts "some text".

So, does this mean every element in the DOM tree is converted to a property on the global object? And does it also mean one can use this as a replacement for the getElementById method in these browsers?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • 10
    See also [Why don't we just use element IDs as identifiers in JavaScript?](http://stackoverflow.com/q/25325221/1048572) on why this should not be used, and [Is there a spec that the id of elements be made global variable?](http://stackoverflow.com/q/6381425/1048572) on how it is spec'd. – Bergi Sep 01 '14 at 15:38
  • 3
    @Bergi, the comment which states to not do this it now outdated and even invalid. Therefore, I cannot find a concrete reason to not use this feature. – ESR Aug 28 '17 at 03:04
  • 2
    @EdmundReed You might want to read the answer of the linked question again - it's still a bad idea: "*implicitly-declared global variables*" have bad to no tooling support and "*lead to brittle code*". Don't call it a "feature", the answer below explains how it's just a bug that became part of the standard for compatibility reasons. – Bergi Aug 28 '17 at 16:07
  • 2
    @Bergi fair enough, you're right. I still think it's a really neat feature though, and is only considered problematic because people aren't aware of it. This is how I envision using it: https://codepen.io/esr360/pen/WEavGE?editors=1000#0 – ESR Aug 29 '17 at 00:22
  • 1
    @EdmundReed It's less problematic if you don't properly separate content and logic of course. Also I recommend to never use inline event handlers or install custom methods on DOM elements abusing them as namespaces (notice it's not a "scope"). – Bergi Aug 29 '17 at 00:27

6 Answers6

471

What is supposed to happen is that ‘named elements’ are added as apparent properties of the document object. This is a really bad idea, as it allows element names to clash with real properties of document.

IE made the situation worse by also adding named elements as properties of the window object. This is doubly bad in that now you have to avoid naming your elements after any member of either the document or the window object you (or any other library code in your project) might want to use.

It also means that these elements are visible as global-like variables. Luckily in this case any real global var or function declarations in your code shadow them, so you don't need to worry so much about naming here, but if you try to do an assignment to a global variable with a clashing name and you forget to declare it var, you'll get an error in IE as it tries to assign the value to the element itself.

It's generally considered bad practice to omit var, as well as to rely on named elements being visible on window or as globals. Stick to document.getElementById, which is more widely-supported and less ambiguous. You can write a trivial wrapper function with a shorter name if you don't like the typing. Either way, there's no point in using an id-to-element lookup cache, because browsers typically optimise the getElementById call to use a quick lookup anyway; all you get is problems when elements change id or are added/removed from the document.

Opera copied IE, then WebKit joined in, and now both the previously-unstandardised practice of putting named elements on document properties, and the previously-IE-only practice of putting them on window are being standardised by HTML5, whose approach is to document and standardise every terrible practice inflicted on us by browser authors, making them part of the web forever. So Firefox 4 will also support this.

What are ‘named elements’? Anything with an id, and anything with a name being used for ‘identifying’ purposes: that is, forms, images, anchors and a few others, but not other unrelated instances of a name attribute, like control-names in form input fields, parameter names in <param> or metadata type in <meta>. ‘Identifying’ names are the ones that should be avoided in favour of id.

edddd
  • 433
  • 3
  • 17
bobince
  • 528,062
  • 107
  • 651
  • 834
  • “are being standardised by HTML5, whose approach is to document and standardise every terrible practice inflicted on us by browser authors, making them part of the web forever.” Generally +1 for that rant. However, +the spec authors also cleaned some mess up, but sometimes it seems, they give in to personal bias, to opinions, to broad usage, etc. – Volker E. May 01 '14 at 20:44
  • 23
    WHY!? Is there anything we can do to stop this madness? My functions got redefined by references to elements and it took an hour for me to debug. :( – Farzher May 08 '14 at 15:26
  • 2
    Can we have some TLDR for this post, and maybe update to 2016? Would it be like "Having 'named elements' exposed to global/window/document scope was a bad idea for the browsers to implement. And relying on this feature should be avoided. [TODO: what would be the advice for naming elements to avoid name clashes? eg. can I name my DIV#location?]'? – Dimitry K Jan 06 '16 at 12:19
  • @CrescentFresh: *"Firefox only does this when put into quirks mode"* Not any more, since it's been standardized. – T.J. Crowder Oct 30 '16 at 17:45
  • 1
    I was curious to see the performance difference and I think this https://jsperf.com/global-named-element/1 shows that it's dramatic, unless I'm missing something. – Jason Sperske May 19 '17 at 17:13
  • You've already linked the relevant section of the W3C spec, but the corresponding section of the HTML Living Standard is here: https://html.spec.whatwg.org/multipage/window-object.html#named-access-on-the-window-object – johncip Feb 26 '19 at 06:33
  • 2
    Fun fact: These global properties also carry over into the """isolated""" execution context of browser extension code, and the LastPass extension got [fully owned](https://bugs.chromium.org/p/project-zero/issues/detail?id=1225) due to this amazing feature – a cat Nov 06 '21 at 02:22
  • Fortunately, a lot of the variable–property clobbering can be mitigated by using ES6 modules or at least strict mode, and `const` and `let`. – Sebastian Simon Feb 05 '22 at 15:41
  • 2
    I agree using document.getElementById is best practice, however I through I would test performance to see if there's a benefit of doing it differently. It appears is indeed to most performant for a modern browser. Here's the simple test I ran to evaluate this: https://jsben.ch/AZD81 – jjr2000 Feb 16 '22 at 18:52
  • So it might have been a bad idea some decades ago, but the variables are still gonna be there and certain precautions have to be made whether you're using them or not. The browsers opened Pandoras box, and you guys are all trying to close it. Spoiler alert: You can't. Why not embrace the feature when it's here. Having consistent names from HTML to JavaScript helps writing better code IMO. And I would recommend having the same values for ids as names. And you wouldn't put id's on tags you're not using, so if you're gonna name them the same anyway, why write that extra line? – agiopnl Mar 07 '22 at 20:26
  • new performance comparison: https://jsben.ch/bexDw – daslicht Apr 18 '22 at 17:29
  • @agiopnl the problem is, it's not very consistent. Suppose you had an element with id `parent` ... – SamB Dec 05 '22 at 00:41
  • If I consistently use it, it's just as consistent as if I consistently don't use it. There just happens to be reserved words in JavaScript. I can't ever imagining spending more time on debugging the possibility of id-ing my element "parent" etc. (it's 0 seconds), than what you spend double-declaring elements 10-100 times for every project you work on throughout your life. Also my code is slightly faster! Life is too short for getting elements by id! – agiopnl Dec 05 '22 at 01:05
  • I forgot to mention how nice it is that DOM elements sticks out from other variables when you prefix them with "window." – agiopnl Dec 05 '22 at 01:14
76

As mentioned in the earlier answer this behavior is known as named access on the window object. The value of the name attribute for some elements and the value of the id attribute for all elements are made available as properties of the global window object. These are known as named elements. Since window is the global object in the browser, each named element will be accessible as a global variable.

This was originally added by Internet Explorer and eventually was implemented by all other browsers simply for compatibility with sites that are dependent on this behavior. Interestingly, Gecko (Firefox's rendering engine) chose to implement this in quirks mode only, whereas other rendering engines left it on in standards mode.

However, as of Firefox 14, Firefox now supports named access on the window object in standards mode as well. Why did they change this? Turns out there's still a lot of sites that rely on this functionality in standards mode. Microsoft even released a marketing demo that did, preventing the demo from working in Firefox.

Webkit has recently considered the opposite, relegating named access on the window object to quirks mode only. They decided against it by the same reasoning as Gecko.

So… crazy as it seems this behavior is now technically safe to use in the latest version of all major browsers in standards mode. But while named access can seem somewhat convenient , it should not be used.

Why? A lot of the reasoning can be summed up in this article about why global variables are bad. Simply put, having a bunch of extra global variables leads to more bugs. Let's say you accidentally type the name of a var and happen to type an id of a DOM node, SURPRISE!

Additionally, despite being standardized there are still quite a few discrepancies in browser's implementations of named access.

  • IE incorrectly makes the value of the name attribute accessible for form elements (input, select, etc).
  • Gecko and Webkit incorrectly do NOT make <a> tags accessible via their name attribute.
  • Gecko incorrectly handles multiple named elements with the same name (it returns a reference to a single node instead of an array of references).

And I'm sure there's more if you try using named access on edge cases.

As mentioned in other answers use document.getElementById to get a reference to a DOM node by its id. If you need to get a reference to a node by its name attribute use document.querySelectorAll.

Please, please do not propagate this problem by using named access in your site. So many web developers have wasted time trying to track down this magical behavior. We really need to take action and get rendering engines to turn named access off in standards mode. In the short term it will break some sites doing bad things, but in the long run it'll help move the web forward.

If you're interested I talk about this in more detail on my blog - https://www.tjvantoll.com/2012/07/19/dom-element-references-as-global-variables/.

TJ VanToll
  • 12,584
  • 4
  • 43
  • 45
  • 5
    Just a note to the obvious caveat to the premise that "it should not be used". That is, "it should not be used UNLESS you happen to be a code cowboy." Code cowboys just go for it. – Jeremy Foster Sep 13 '14 at 00:19
  • 6
    @jeremyfoster unless "code cowboy" means someone who uses and propagates bad developer-unfriendly implementations, I strongly disagree. – Patrick Roberts Jun 28 '15 at 05:30
  • 3
    One mark of a good cowboy is that many disagree. But now I'm like the philosophical cowboy or something like that. – Jeremy Foster Jun 28 '15 at 15:49
  • 1
    More people should be using `document.querySelectorAll` and `document.querySelector` when accessing the DOM. +1 for the good suggestion of using that. Accessing elements by selector is definitely a more efficient process. – Travis J Oct 28 '15 at 23:14
  • @TravisJ Only if id is not available, otherwise it's, as well as `getElementsByClassName`, etc twice slower than `getElementById` – vanowm Apr 03 '22 at 15:46
  • You are argumenting against browsers to implement this behavior, not against using it. Whether you like it or not this behavior won't reverse, it's naive to think otherwise! Might as well embrace it. I don't know how your arguments about the name attribute is relevant for op's question? – agiopnl Jun 11 '22 at 15:32
23

You should stick to getElementById() in these cases, for example:

document.getElementById('example').innerHTML

IE likes to mix elements with name and ID attributes in the global namespace, so best to be explicit about what you're trying to get.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
14

The question should sound:: "Do HTML Tags with provided IDs become globally accessible DOM Elements?"

The answer is YES!

That's how it was meant to work, and that's why IDs were introduced by W3C to begin with.: The ID of an HTML Tag in a parsed scripting environment becomes its corresponding DOM Element handle.

However, Netscape Mozilla refused to conform to (to them intruding) W3C and stubbornly kept using the deprecated Name attribute to create havoc and therefore break the Scripting functionality and the coding convenience brought in by the W3C's introduction of Unique IDs.

After the Netscape Navigator 4.7 fiasco their developers all went and infiltrated the W3C, whereas their associates were superseding the Web with wrong practices and misusing examples. Forcing the use and reuse of already deprecated Name attribute [!which was not meant to be unique] on par with ID attributes so that scripts that utilized ID handles for accessing particular DOM elements would simply break!

And break they did as they would also write and publish extensive coding lessons and examples [their browser would not recognize anyway] such as document.all.ElementID.property instead of ElementID.property to at least make it inefficient and give the browser more overhead in case it didn't simply break it at HTML domain by using the same token for the (now [1996-97], deprecated) Name and the standard ID attribute supplying it with the same token value.

They easily managed to convince the - back then - overwhelming army of ignorant code-writing amateurs that Names and IDs are practically the same, except that ID attribute is shorter and therefore byte-saving and more convenient to the coder than the ancient Name property. Which was of course a lie. Or - in their superseding published articles of HTML, convincing articles that you'll need to provide both Name and ID to your tags for them to be accessible by the Scripting engine.

Mosaic Killers [codenamed "Mozilla"] were so pissed they thought "if we go down, so should Internet".

The rising Microsoft - on the other hand - were so naive they thought they should keep the deprecated and marked for deletion Name property and treat it as if it was an ID that is a unique Identifier so that they wouldn't break the scripting functionality of old pages coded by Netscape trainees. They were deadly wrong...

And the returning of an array collection of ID conflicting elements was not a solution to this deliberate man-made problem either. Actually it defeated the whole purpose.

And this is the sole reason W3C turned ugly and gave us idiocies such as document.getElementById and the accompanying rococo goddamn annoying syntax of the sort... (...)

Bekim Bacaj
  • 5,707
  • 2
  • 24
  • 26
  • 2
    So in 2021 I'm thinking it's safe to use this for small scale websites & forms? What is the percentage of real prospective clients not being able to submit a form that doesn't use document.getElementById() ? To me, in 2021 this seems like a big time saver and for all intensive purposes is safe to use? As long as the developer knows exactly what is going on in the small level use of code. – drooh Nov 18 '21 at 16:12
  • 1
    This was interesting, I don't like rococo code either. I didn't quite understand the total history. How were elements accessed before getElementById? – agiopnl Mar 07 '22 at 20:13
  • 2
    @agiopnl They were accessed by their assigned IDs! That was the sole reason that WC3 introduced IDs over Names. IDs are supposed to be unique. Whereas Names are not. Just like in real life. – Bekim Bacaj Apr 15 '22 at 11:38
5

Yes, they do.

Tested in Chrome 55, Firefox 50, IE 11, IE Edge 14, and Safari 10
with the following example:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <div id="im_not_particularly_happy_with_that">
    Hello World!
  </div>
  <script>
    im_not_particularly_happy_with_that.innerText = 'Hello Internet!';
  </script>
  <!-- Looking at you W3 HTML5 spec group ಠ_ಠ -->
</body>
</html>

http://jsbin.com/mahobinopa/edit?html,output

qff
  • 5,524
  • 3
  • 37
  • 62
1

document.all needs to be un-deprecated and fixed up a bit

Elements with an id or name are available in document.all, which was exciting news to me.

However, from what I've gathered all is deprecated and spit upon by old-timers, BUT no one seems to be able to provide any compelling reason to never use it, or at least a strong enough argument for why it should be deprecated.

I'm using it for a little internal tool page and it totally works...for now. It does have problems, but the convenience and cleanliness can't be ignored:

Given

<h1 id="welcomeMessage" hidden>Welcome!</h1>

You can do

document.all.welcomeMessage.hidden = false

vs.

document.getElementById('welcomeMessage').hidden = false
document.querySelector('#welcomeMessage').hidden = false

So nice! But there's some problems:

  • If there happens to be more than one element with the same id (possible, but can and should be avoided) or name (very possible and can't be avoided because of checkbox groups), then an HTMLCollection is returned, not a single Element
  • If you for some reason try to get an element with an id or name that matches inherited properties like __proto__ or toString, then you'll get those objects instead of undefined
  • It's not as fast as document.getElementById, so there's performance concerns if you're querying tons of elements

A nice alternative

I decided to do this instead and I'm not really sure how late I am to the party, but I'm lovin' this:

const elements = new Proxy({}, {
  get(target, prop) {
    return document.getElementById(prop) || document.getElementsByName(prop)[0];
  }
});

// Clean and direct access to elements just like document.all
elements.welcomeMessage.hidden = false

jordanfb
  • 539
  • 5
  • 9