Skip to content

Instantly share code, notes, and snippets.

@finom
Created August 22, 2014 13:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finom/3ecfcea07d95f5dd7f8a to your computer and use it in GitHub Desktop.
Save finom/3ecfcea07d95f5dd7f8a to your computer and use it in GitHub Desktop.
Simple addEventListener polyfill for IE8
( function( win, doc, s_add, s_rem ) {
if( doc[s_add] ) return;
Element.prototype[ s_add ] = win[ s_add ] = doc[ s_add ] = function( on, fn, self ) {
return (self = this).attachEvent( 'on' + on, function(e){
var e = e || win.event;
e.target = e.target || e.srcElement;
e.preventDefault = e.preventDefault || function(){e.returnValue = false};
e.stopPropagation = e.stopPropagation || function(){e.cancelBubble = true};
e.which = e.button ? ( e.button === 2 ? 3 : e.button === 4 ? 2 : e.button ) : e.keyCode;
fn.call(self, e);
});
};
Element.prototype[ s_rem ] = win[ s_rem ] = doc[ s_rem ] = function( on, fn ) {
return this.detachEvent( 'on' + on, fn );
};
})( window, document, 'addEventListener', 'removeEventListener' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment