Last active
February 17, 2017 14:05
-
-
Save regou/9224427 to your computer and use it in GitHub Desktop.
Custom event polyfill
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
var Event={ | |
// Trigger a new event | |
trigger: function(eventType, data, bubbles, cancelable) { | |
var event = new CustomEvent(eventType, { | |
detail: data, | |
bubbles: !!bubbles, | |
cancelable: !!cancelable | |
}); | |
// Make sure to trigger the event on the given target, or dispatch it from | |
// the window if we don't have an event target | |
data && data.target && data.target.dispatchEvent(event) || window.dispatchEvent(event); | |
}, | |
// Bind an event | |
on: function(type, callback, element) { | |
var e = element || window; | |
// Otherwise bind a normal event | |
e.addEventListener(type, callback); | |
}, | |
off: function(type, callback, element) { | |
element.removeEventListener(type, callback); | |
} | |
} |
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
// custom event polyfill for IE9 - IE10 | |
(function () { | |
function CustomEvent (event, params) { | |
params = params || { bubbles: false, cancelable: false, detail: undefined }; | |
var evt = document.createEvent('CustomEvent'); | |
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail); | |
return evt; | |
}; | |
CustomEvent.prototype = window.CustomEvent.prototype; | |
window.CustomEvent = CustomEvent; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment