Skip to content

Instantly share code, notes, and snippets.

@regou
Last active February 17, 2017 14:05
Show Gist options
  • Save regou/9224427 to your computer and use it in GitHub Desktop.
Save regou/9224427 to your computer and use it in GitHub Desktop.
Custom event polyfill
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);
}
}
// 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