Skip to content

Instantly share code, notes, and snippets.

@derekpeterson
Created May 29, 2014 21:30
Show Gist options
  • Save derekpeterson/8fda2b4495984953ac2a to your computer and use it in GitHub Desktop.
Save derekpeterson/8fda2b4495984953ac2a to your computer and use it in GitHub Desktop.
if ( 'function' !== typeof window.addEventListener ) {
(function ( win, doc ) {
function docHijack( p ) {
var old = doc[p];
doc[ p ] = function ( v ) {
return addListen( old( v ) );
};
}
function addEvent ( on, fn, self ) {
return ( self = this ).attachEvent( 'on' + on, function ( e ) {
var ev = e || win.event;
ev.preventDefault = ev.preventDefault || function () {
ev.returnValue = false;
};
ev.stopPropagation = ev.stopPropagation || function () {
ev.cancelBubble = true;
};
fn.call( self, ev );
});
}
function addListen ( obj, i ) {
i = obj.length;
if ( i ) {
while ( i-- ) {
obj[ i ].addEventListener = addEvent;
}
} else {
obj.addEventListener = addEvent;
}
return obj;
}
addListen( [ doc, win ] );
if ( 'Element' in win ) {
// IE8
win.Element.prototype.addEventListener = addEvent;
} else {
//IE < 8
//Make sure we also init at domReady
doc.attachEvent( 'onreadystatechange', function () {
addListen( doc.all );
});
docHijack( 'getElementsByTagName' );
docHijack( 'getElementById' );
docHijack( 'createElement' );
addListen( doc.all );
}
}( window, document ));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment