Skip to content

Instantly share code, notes, and snippets.

@KidkArolis
Created October 10, 2013 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KidkArolis/6919254 to your computer and use it in GitHub Desktop.
Save KidkArolis/6919254 to your computer and use it in GitHub Desktop.
John Resig's on/off (aka addEventListener)
function on(obj, type, fn) {
if (obj.attachEvent) {
obj['e'+type+fn] = fn;
obj[type+fn] = function(){ obj['e'+type+fn]( window.event ); };
obj.attachEvent( 'on'+type, obj[type+fn] );
} else {
obj.addEventListener( type, fn, false );
}
}
function off(obj, type, fn) {
if (obj.detachEvent) {
obj.detachEvent('on'+type, obj[type+fn]);
obj[type+fn] = null;
} else {
obj.removeEventListener(type, fn, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment