Skip to content

Instantly share code, notes, and snippets.

@Zirak
Created November 8, 2011 17:11
Show Gist options
  • Select an option

  • Save Zirak/1348400 to your computer and use it in GitHub Desktop.

Select an option

Save Zirak/1348400 to your computer and use it in GitHub Desktop.
var listener = function ( fun ) {
return function ( e ) {
//thank you IE for the wonderful fish
e = e || window.event;
e.target = e.target || e.srcElement;
e.stopPropagation = e.stopPropagation || function () {
this.cancelBubble = true;
};
e.preventDefault = e.preventDefault || function () {
this.returnValue = false;
};
//because the IE event model will pass window as the `this` value, we'll have to select the element ourselves (not that it's that hard)
//did I mention IE doesn't have .currentTarget, and it's probably necessary to reimplement event bubbling? yeah, thought so
return fun.call( e.currentTarget, e );
};
};
var addListener = function ( elem, type, fun ) {
if ( elem.addEventListener ) {
elem.addEventListener( type, listener(fun), false );
}
else if ( elem.attachEvent ) {
elem.attachEvent( 'on' + type, listener(fun) );
}
};
//since `fun` is called indirectly, via `listener`, how is it possible to remove a specific listener?
//there's the possibility of going into giant arrays, but it's probably best not to
var removeListener = function ( elem, type, fun ) { };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment