Safari2 de HTMLElement Prototyping
Safari2(1は知らない)の話ですがfxにあるHTMLElement.prototypeをいじれないかなって調べてみました。Safari2でもHTMLElement.prototypeみたいなことができそうです。
対応表
fxでのそれ | Safari2でのそれ | おまじない |
---|---|---|
Document.prototype | window['[[DOMDocument.prototype ]]'] | document.createElement(*) or document; |
HTMLElement.prototype | window['[[DOMElement.prototype]]'] | document.createElement(*) |
HTMLCollection.prototype | window['[[HTMLCollection.prototype]]'] | document.links; |
- 上の表記では[[]]が全角になっていますが実際は半角です。はてな記法に引っかかるために全角で表示。
- おまじないは下記参照。他にもありそうだけど追々調べる。
ただちょっと
利用するにはおまじないが必要でした。mootoolsのソースを読み漁っていた結果取得する前段階でdocument.createElement('iframe');しないと undefined
alert(window['[[DOMElement.prototype]]']); // undefined // 以下のおまじないが必要 via mootools (htmlとかでもできた) document.createElement('iframe'); alert(window['[[DOMElement.prototype]]']); // [object DOMElement]
スニペット
document.createElement('iframe'); function HTMLElement(){}; HTMLElement.prototype = window['[[DOMElement.prototype]]']; // 以下でもいいかも。 // var HTMLElement = {prototype : window['[[DOMElement.prototype]]']};
サンプルコード
<html> <head> <script type="text/javascript"> document.createElement('iframe'); HTMLElement = {prototype : window['[[DOMElement.prototype]]']}; HTMLElement.prototype.hoge = function(str){ alert(str); }; window.onload = function(){ document.getElementById('foo').hoge("Hello World!!"); // alert("Hello World!!"); }; </script> </head> <body> <div id="foo">foo</div> </body> </html>