[whatwg] [WHATWG] HTMLElement ids as global object properties

Randy Drielinger Randy at ProWebDesign.nl
Mon Apr 4 00:34:14 PDT 2011


Funny... IE9 shows true, but only when there's a valid doctype :)



-----Original Message----- 
From: Boris Zbarsky
Sent: Monday, April 04, 2011 12:58 AM
To: Alexandre Morgaut
Cc: whatwg at lists.whatwg.org
Subject: Re: [whatwg] [WHATWG] HTMLElement ids as global object properties

On 4/3/11 12:53 PM, Alexandre Morgaut wrote:
>> The id lookup happens after all other property resolution in browsers
>> (but NOT in the current spec text, note), so if you had<div
>> id="location">  and accessed window.location you would get a Location
>> object, not the<div>.
>
> So you can not rely on being able to access an HTMLElement from its id 
> this way...
> (As any Javascript code could use a global variable with the same
> name/id)

Or a future spec revision could add a new property on Window.prototype.
  Or on Object.prototype.  Try this testcase (the lack of a doctype is
important in Gecko's case):

<body>
   <div id="valueOf">aaa</div>
   <script>alert(window.valueOf == Object.prototype.valueOf)</script>
</body>

This alerts false in Opera, but true in Gecko, Safari, Chrome.  I don't
have IE on hand to test right now.

So yes, this is a very fragile way of accessing elements.

> Is there any registered bug on Opera and Webkit about this topic ?

I'm not aware of any....

-Boris 




More information about the whatwg mailing list