どのイベントバインディングが最良か?
On the one hand, the tag attribute approach is acknowledged by the W3C DOM Level 2 recommendation as an acceptable substitute for the addEventListener() method. To be compatible with millions of existing scripts, all scriptable browsers support tag attribute binding. Automated authoring tools, such as DreamWeaver, also embed event handler attributes in HTML tags.
IE5 for Mac(OS9.x)のJavaScriptにはaddEventListenerとか無くて、「このJavaScriptはMacには(ry」なんて良くある。クロスブラウザなJavaScriptなんてやっぱり現実的じゃないのかな?ってADCのドキュメントを眺めてたら有りました。解決方法。
タグ属性のアプローチでいいのか!てっきりIE独自機能かと思い込んでた。W3C DOM Level2推薦で承諾されてたとは。
ADCのデモを今手元にあるブラウザでチェック。Macも含めて問題なし。ちょっと感動した。prototype.js使わずにaddEventListenerっぽいのを自分で書いた方が良さそう。
ちなみに
elem.onmouseover = toggleHighlight elem.onmouseout = toggleHighlight
は
elem['onmouseover'] = toggleHighlight elem['onmouseout'] = toggleHighlight
とも書ける。
これでHTMLとSCRIPTの分離がほぼ完璧だぁ〜。しかも手間大幅半減。Viva! ADC
*追記:MacIEのattachEvent
MacIEにはattachEventが一応あるけどwindow.attachEventとdocument.attachEventしかなく他の要素にはattachEventはない。