Monday, March 6th, 2006

Forget addEvent, use Yahoo!’s Event Utility

Category: Articles, Examples, JavaScript, Library

Dustin Diaz thinks that we should forget addEvent, use Yahoo!’s Event Utility. This comes from a guy who has watched others, and who gave it a shot himself.

Why should we use it?

  • Handler Attachment Deferral: According to the event utility API document: If you attempt to attach a handler to an element before the page is fully loaded, the Event component will attempt to locate the element. If the element is not available, Event periodically checks for the element until the window.onload event is triggered. Oh my F’Gosh! Is that not dope? In a way, it cleans up your mess just incase you goofed.
  • Automatic Scope Correction: By default in Internet Explorer (using the attachEvent method) when you look at the this object within your callback function, it looks at the window object whereas according to DOM2 methods it should be the element from which triggered the event. The YUI Event.addListener method corrects this behavior and appropriately changes the scope for you.
  • Automatic Event Object Browser Abstraction: What this means is that we no longer need to look at window.event when sniffing for the event type. This is automatically passed in as the first parameter into the callback function.
  • Send Any Object: You can pass in an object as a fourth parameter to the addListener method which becomes available to you as the second parameter in the callback function

Yahoo! Event Example

Posted by Dion Almaer at 9:48 am
3 Comments

++++-
4 rating from 19 votes

3 Comments »

Comments feed TrackBack URI

> In a way, it cleans up your mess just incase you goofed.

Why not just don’t goof? Webapps are inherently hard to debug. Yahoo just made it close to impossible. Cheer!

Comment by Laurens van den Oever — March 6, 2006

Handler Attachment Deferral: bad practice because it tries to clean up your mess in case you goofed
Automatic Scope Correction: that’s where you have closures for
Automatic Event Object Browser Abstraction: agreed, but simple to do
Send Any Object: that’s where you have closures for

Comment by Lon — March 6, 2006

I gotta say, I completely agree. You don’t have to use the deferral if you don’t want to, so that’s a matter of taste, and very valuable when other solutions are too complicated. The automatic scope correction is the feature that sold me. It’s just so much easier. Yahoo’s library is very well thought out.

Comment by Mark Kawakami — March 6, 2006

Leave a comment

You must be logged in to post a comment.