Friday, October 21, 2005

addEvent() recoding contest

Adding events to elements via javascript should be easy but is not. Each browser has different methods of adding events, most standards compliant ones would use addEventLister() (as defined by the W3C), but Internet Explorer uses attachEvent(). That is why Scott Andrew LePera created functions to add and remove events, regardless of browser: Crossbrowser DOM Scripting: Event Handlers.

However, Peter-Paul Koch of QuirksMode found a problem with it: addEvent() considered harmful. So he set up a recoding contest for developers to improve on it. Eventually a winner was declared, but there are still problems with it. It does not work on IE5 Mac or Netscape 4 (although with the number of people still using them dropping, it may not cause a problem as long as you have a fallback, like server-side processing), plus there can still be a memory leak with Internet Explorer for Windows.

One of the judges, Dean Edwards decided to write his own solution. It is different, as it does not use addEventLister or attachEvent. But it does work cross browser and does not leak memory. It may not be scalable as addEventLister/attachEvent and also may not work in the future (if element.onclick is no longer supported).

Tags: , ,

No comments: