medBouzid
medBouzid

Reputation: 8382

CustomEvent IE8 support, how to pass the params?

I am trying to make a polyfill for CustomEvent which support IE8 as well, this is what I came up with:

if (typeof window.CustomEvent === "function") {
  return false;
} else {
  function CustomEvent (eventName, params) {
    params = params || { bubbles: false, cancelable: false, detail: undefined };
    if (document.createEvent) { // IE9, IE10
      var evt = document.createEvent('CustomEvent');
      evt.initCustomEvent(
        eventName,
        params.bubbles,
        params.cancelable,
        params.detail
      );
      return evt;
    } else if (document.createEventObject) { // IE8
      var evt = document.createEventObject();
      evt.eventType = eventName;
    }
  };
  CustomEvent.prototype = window.CustomEvent.prototype;
  window.CustomEvent = CustomEvent;
}

For the IE8 part how I can pass the params?

Upvotes: 2

Views: 300

Answers (1)

Michał Perłakowski
Michał Perłakowski

Reputation: 92501

You can iterate over params and assign them to the event object:

var evt = document.createEventObject();
evt.eventType = eventName;

for (var prop in params) {
  evt[prop] = params[prop];
}

Upvotes: 1

Related Questions