-
-
Save alvinkatojr/41b4ed86476a6762a1dabde5d36c220d to your computer and use it in GitHub Desktop.
Init-Time Branching - JavaScript Patterns
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
| // the interface | |
| var utils = { | |
| addListener: null, | |
| removeListener: null | |
| }; | |
| // the implementation | |
| if (typeof window.addEventListener === 'function') { | |
| utils.addListener = function (el, type, fn) { | |
| el.addEventListener(type, fn, false); | |
| }; | |
| utils.removeListener = function (el, type, fn) { | |
| el.removeEventListener(type, fn, false); | |
| }; | |
| } else if (typeof document.attachEvent === 'function') { // IE | |
| utils.addListener = function (el, type, fn) { | |
| el.attachEvent('on' + type, fn); | |
| }; | |
| utils.removeListener = function (el, type, fn) { | |
| el.detachEvent('on' + type, fn); | |
| }; | |
| } else { // older browsers | |
| utils.addListener = function (el, type, fn) { | |
| el['on' + type] = fn; | |
| }; | |
| utils.removeListener = function (el, type, fn) { | |
| el['on' + type] = null; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment