Last active
December 16, 2015 13:59
-
-
Save THEtheChad/5445159 to your computer and use it in GitHub Desktop.
Cross browser implementation for manipulating event hooks.
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
/** | |
* Creates two functions on the window object: | |
* - addEvent | |
* - removeEvent | |
* | |
* @author THEtheChad | |
*/ | |
/** | |
* attachEvent: Binds an event handler to the given element | |
* @param {object} The element you wish to bind the handler to | |
* @param {string} The type of event you're listening for | |
* @param {function} The handler you wish to execute | |
*/ | |
/** | |
* removeEvent: Removes an event handler from the given element | |
* @param {object} The element you wish to remove the handler from | |
* @param {string} The type of event the handler is attached to | |
* @param {function} The handler you wish to remove | |
*/ | |
(function(window){ | |
// Refactored code originally written by John Resig | |
// http://ejohn.org/projects/flexible-javascript-events/ | |
if(document.attachEvent){ | |
window.addEvent = function(obj, type, fn){ | |
obj['e'+type+fn] = fn; | |
obj[type+fn] = function(){obj['e'+type+fn]( window.event )} | |
obj.attachEvent('on'+type, obj[type+fn]); | |
} | |
window.removeEvent = function(obj, type, fn){ | |
obj.detachEvent( 'on'+type, obj[type+fn] ); | |
obj[type+fn] = null; | |
} | |
} | |
else{ | |
window.addEvent = function(obj, type, fn){ obj.addEventListener(type, fn, false) } | |
window.removeEvent = function(obj, type, fn){ obj.removeEventListener(type, fn, false) } | |
} | |
}).call(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment