Browser’s implied globals

Like it's not bad enough that JavaScript has implied globals (forget var and you create a global), but the browsers have decided it's a good idea to add more pollution to the global namespace.

This has been a source of frustration before with IE, it's really hard to understand the logic behind it, but it's also happening in other browsers.

Consider this:

<meta name="description" content="test me" />

A normal META tag, right? But in IE this will create in a global variable called "description" pointing to that DOM node. Yep.

alert(description.content); // "test me"

That's pretty annoying. Even more annoying is that getElementById('description') will also point to the DOM node, although it doesn't even have an ID.

A test is born

Anyway, I wanted to test the effect of other name and id attributes in different tags and different browsers. With the exception of Firefox which doesn't create any globals, all other did to some degree. Rather disappointing. I tested IE6, 8 (plus compat view), FF 3.5, Safari 4 and Opera 10.

Here's the test page

And below are the results. The yellow x means that testing for the presence of this global returned "undefined", the white o means that the global variable points to an object. So for example continuing with the meta example above, typeof window.description will return undefined in FF (yellow x) and object in IE (white o).

global description IE FF Saf O
description <meta name="description"... o x x o
robots <meta name="robots"... o x x o
paragraph-id <p id="paragraph-id"... o x o o
paragraph-name <p name="paragraph-name"... x x x o
form-name <form name="form-name"... o x o o
form-id <form id="form-id"... o x o o
input-name <input name="input-name"... x x x x
input-id <input id="input-id"... x x o x
link-name <a name="link-name"... o x x o
link-id <a id="link-id"... o x o o
div-name <div name="div-name"... x x x o
div-id <div id="div-id"... o x o o

So...?

So this is a useless feature if you ask me. Not reliable, not cross-browser, maybe considered convenient back when rollover buttons and animated gifs were all the rage (and animated window.status, remember?), but today can only cause troubles where you least expect it. Should be removed in future browser versions.

For the time being we just have to remember to always declare and initialize our local variables because it looks like someone else might also decide to do so for us. Which can lead to errors if we assume too much.

This entry was posted on Tuesday, October 20th, 2009 and is filed under browsers, firefox, IE, JavaScript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

18 Responses to “Browser’s implied globals”

  1. Philip Tellis Says:

    Opera creates window.opera

  2. David Henderson Says:

    Hi Stoyan, for completeness, Google Chrome unsurprisingly mirrors the results from Safari…

    paragraph-id: object
    form-name: object
    form-id: object
    input-id: object
    link-id: object
    div-id: object

  3. Maciej Łebkowski Says:

    I think the most interesting thing is: does those global variables get declared when user inserts node dynamically, from Javascript. This could lead to overwriting existing variables.

    BTW, why can’t I type „dunno” in spam protection field? :-)

  4. Twitter Trackbacks for Browser’s implied globals / Stoyan's phpied.com [phpied.com] on Topsy.com Says:

    [...] Browser’s implied globals / Stoyan’s phpied.com http://www.phpied.com/browser-implied-globals – view page – cached Like it’s not bad enough that JavaScript has implied globals (forget var and you create a global), but the browsers have decided it’s a good idea to add more pollution to the global namespace. — From the page [...]

  5. TwittLink - Your headlines on Twitter Says:

    [...] Tweets about this great post on TwittLink.com [...]

  6. Alexandre Morgaut Says:

    Sure this shouldn’t be mapped as implied globals…

    Could we have a “document.meta” collection instead ?
    Or even “document.head.meta” ?

    For the others examples (form, div, input, p, from id or name), it makes me think to forms, elements, anchors, links, and images HTMLCollections

    It is also possible to call (I didn’t already tested it on all browsers) :
    document.forms[form-name].elements[input-name]
    This can provide some unespected results if a form or an input element has some specific name values like:
    – “id”
    – “length”
    – “action”
    – “name”

  7. Stepan Reznikov Says:

    Hi Stoyan,

    Try changing to and then running console.log(paragraph.tagName) in Firefox. You will get “P” (surprise!) Firefox does this in Quirks mode, but not in Standards mode.

  8. Stepan Reznikov Says:

    Hi Stoyan,

    Try changing id=”paragraph-id” to id=”paragraph” and then running console.log(paragraph.tagName) in Firefox. You will get “P” (surprise!) Firefox does this in Quirks mode, but not in Standards mode.

  9. Gonzo Says:

    The issue with name attribute acting as an id in IE is well known for form elements, but not surprisingly it appears valid for any element. I guess the other browsers suffering from this illness just copied the behavior from IE.

  10. Ajaxian » Implied globals in browsers Says:

    [...] Stefanov has done some testing on so called implied globals in browsers. One of the interesting finds was that the meta description is accessible in JavaScript using [...]

  11. Implied globals in browsers | Guilda Blog Says:

    [...] Stefanov has done some testing on so called implied globals in browsers. One of the interesting finds was that the meta description is accessible in JavaScript using [...]

  12. Implied globals in browsers Says:

    [...] Stefanov has done some testing on so called implied globals in browsers. One of the interesting finds was that the meta description is accessible in JavaScript using [...]

  13. Implied globals in browsers » Intipadi.com - Majalah Sains dan Teknologi Informasi Says:

    [...] – Stoyan Stefanov has done some testing on so called implied globals in browsers. One of the interesting finds was that the meta description is accessible in JavaScript using [...]

  14. Tech Thursday - packing, shooting, speaking, and pranking your colleagues - ComponentGear.com Feed - ComponentGear.com Says:

    [...] the footsteps of Firefox and supports WebGL to allow for 3D web development.Did you know that a meta description in IE becomes a global variable in JavaScript? Stoyan Stefanov has done some testing.Cameramouse is an interesting tool that allows you to [...]

  15. Implied globals in browsers - Wasif Hafeez Says:

    [...] Stefanov has done some testing on so called implied globals in browsers. One of the interesting finds was that the meta description is accessible in JavaScript using [...]

  16. peter Says:

    good work

    however, removing this will never happen. the argument will be “it will break the web”
    But it is a good example of the mess we are in

  17. Iouri Goussev Says:

    Internet explorer must die, its a plague sent to kill netscape but it also poisoned internet!

  18. tory burch outlet store Says:

    Jiangdong starting shook his head, sighed heavily, is not to find Zhao, and sat down once again. The eyes of a number of dark, some lost, some restless. Zhao Xin see tory burch flats appear so sad, the hearts of tenderness at this point and said: Well, tory burch outlet to discuss upstairs. From Jiangdong not discussing, the two one after the previous one went downstairs and one upstairs. Jiangdong from the conclusion with Zhao came to her and also the common Suhongtu bedroom.

Leave a Reply