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

Boris Zbarsky bzbarsky at MIT.EDU
Sun Apr 3 15:58:52 PDT 2011


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