W3C home > Mailing lists > Public > whatwg@whatwg.org > April 2011

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

From: Randy Drielinger <Randy@ProWebDesign.nl>
Date: Mon, 4 Apr 2011 09:34:14 +0200
Message-ID: <91BF91D9B80240C583FADE0E44B1F8B7@emea.inin.com>
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 
Received on Monday, 4 April 2011 00:34:14 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 30 January 2013 18:48:03 GMT