Created
October 10, 2013 14:25
-
-
Save KidkArolis/6919254 to your computer and use it in GitHub Desktop.
John Resig's on/off (aka addEventListener)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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