Flexible Javascript Events


This is the project page for my entry into the addEvent() recoding contest. It works in all of the modern browsers: Windows IE 5+, Mozilla, Opera, and Safari. The code meets every item outlined in the guideline – attempting to be as short and simple as possible. You can view a demo of it in action.

This library consists of two functions: addEvent and removeEvent. To use them, simply copy the code from below, paste it into your Javascript code, and call it using these methods:

addEvent( object, eventType, function );

The ‘object’ parameter should be the object upon which you want the event to be called.
The ‘eventType’ parameter should hold the name of the event, for example: ‘click’, ‘mouseover’, ‘load’, ‘submit’, etc. More can be found here.
The ‘function’ parameter should be a reference to a function that you wish to have called whenever the event fires. One parameter will be passed to ‘function’ – the event object.

Some examples of addEvent in use:

addEvent( document.getElementById('foo'), 'click', doSomething );
addEvent( obj, 'mouseover', function(){ alert('hello!'); } );
removeEvent( object, eventType, function );

removeEvent is virtually identical to addEvent, with the obvious difference: Instead of the function being added to the event handler, it is removed instead. All of the above code and parameters applies to this function.

The code, itself, is very short and simple – only 15 lines long:

  1. function addEvent( obj, type, fn ) {
  2.   if ( obj.attachEvent ) {
  3.     obj['e'+type+fn] = fn;
  4.     obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
  5.     obj.attachEvent( 'on'+type, obj[type+fn] );
  6.   } else
  7.     obj.addEventListener( type, fn, false );
  8. }
  9. function removeEvent( obj, type, fn ) {
  10.   if ( obj.detachEvent ) {
  11.     obj.detachEvent( 'on'+type, obj[type+fn] );
  12.     obj[type+fn] = null;
  13.   } else
  14.     obj.removeEventListener( type, fn, false );
  15. }

Much of the above code is trying to fix a serious problem with Internet Explorer. The code has to be this troublesome due to the fact that when your attached function gets fired, the ‘this’ reference refers to the worthless ‘window’, when, in fact, it should refer to the parent object. An explanation of the key points:

obj['e'+type+fn] = fn;

This makes the function a child of the specified object. The key, which is placed in the object hash, is (hopefully) unique and won’t collide with any other function additions.

obj[type+fn] = function(){ obj['e'+type+fn]( window.event ); }

This line creates an anonymous function who, once executed, will fire the previously attached function – passing it the global event object. This whole function is being attached to the object so that it can be removed later, using the removeEvent() function.

Finally, for more information on the attachEvent, detachEvent, addEventListener, and removeEventListener functions – please refer to the excellent resource at Quirksmode.

If you have any additional questions concerning the above code, please feel free to ask.

Posted: September 8th, 2005


If you particularly enjoy my work, I appreciate donations given with Gittip.

68 Comments (Show Comments)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

Ukiyo-e Database and Search

Ukiyo-e.org

Japanese woodblock print database and search engine.


John Resig Twitter Updates

@jeresig

Infrequent, short, updates and links.


via Ad Packs