Created
February 15, 2013 23:24
-
-
Save mattsnider/4964452 to your computer and use it in GitHub Desktop.
Dead simple wrapper for event functions in JavaScript. Adds addListener and removeListener to the global namespace.
This file contains 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(w) { | |
// Create the Event Function wrappers | |
if (w.addEventListener) { | |
// standards compliant method | |
w.addListener = function(el, eType, fn, capture) { | |
el.addEventListener(eType, fn, capture); | |
}; | |
w.removeListener = function (el, eType, fn, capture) { | |
el.removeEventListener(eType, fn, capture); | |
}; | |
} | |
else if (w.attachEvent) { | |
// microsoft compliant method | |
w.addListener = function(el, eType, fn, capture) { | |
el.attachEvent("on" + eType, fn, capture); | |
}; | |
w.removeListener = function (el, eType, fn, capture) { | |
el.detachEvent("on" + eType, fn, capture); | |
}; | |
} | |
else { | |
// really old browses, circa 1990's | |
throw('Event handlers are unsupported in this browser.'); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment