Skip navigation.

My comments on ...

... everything bothering me

Posts tagged with "DOM"

Safari and HTMLElement.prototype

, ,

As you all know, Safari does not support HTMLElement, which makes it hard to add stuff to all newly created HTML-Elements, Nodes, whatever you might call it.

Fortunately, it supports the semi-standard __proto__ property. Exploiting another "bug", that all HTMLElement(s) are produced from the same prototype, I came up with a neat solution that even works in Safari 1.3 and you can replace functions such as "getAttribute" (go figure!).

var w = window;
if(!w.HTMLElement&&(typeof document.createElement)=="function" // check basics
   &&(var t=document.createElement('a').__proto__) // '__proto__' supported?
   &&t==document.createElement('p').__proto__){ // all HTMLElements the same?
   w.HTMLElement={}; // prevent people from constructing
   w.HTMLElement.prototype=t;
}

This piece uses perfect object detection, i.e. every browser that works this way will be fixed (i.e. all supporting Safaris, maybe KHTML?), while leaving browsers that work differently untouched.

Remember: You saw it here first!

PS: Best used inside a closure, e.g. (function(){ [your code here] })().
December 2008
M T W T F S S
November 2008January 2009
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31