Skip to content

Instantly share code, notes, and snippets.

@alexnoz
Created July 23, 2017 06:53
Show Gist options
  • Save alexnoz/425674ac1d9094f2010361fbf73e999b to your computer and use it in GitHub Desktop.
Save alexnoz/425674ac1d9094f2010361fbf73e999b to your computer and use it in GitHub Desktop.
Create custom event in a cross-browser manner. Works in IE >= 9
/**
* @param {String} name - The event's name
* @param {Object} [options] - Options to pass to the event constructor
* @returns Custom event
*/
function getCustomEvent (name, options = {}) {
let event;
if (typeof window.CustomEvent === 'function') {
event = new CustomEvent(name, options);
} else {
event = document.createEvent('CustomEvent');
event.initCustomEvent(
name,
options.bubbles,
options.cancellable,
options.detail
);
}
return event;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment