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

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

From: Boris Zbarsky <bzbarsky@MIT.EDU>
Date: Sun, 03 Apr 2011 18:58:52 -0400
Message-ID: <4D98FBAC.4090601@mit.edu>
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 Sunday, 3 April 2011 15:58:52 GMT

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