Object doesn't support this action IE9 with CustomEvent Initialization

前端 未结 4 1660
时光说笑
时光说笑 2021-02-05 13:29

I am getting the following error in IE9:

\"Object doesn\'t support this action\".

There are various question about this, but mine is specifically for the followi

相关标签:
4条回答
  • 2021-02-05 13:58

    You can use a javascript function to detect if the browser is IE11 or lower then apply the next polyfill:

        (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.Event.prototype;
          window.CustomEvent = CustomEvent;
        })();
    

    The polyfill from above is taken from MDN: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent

    0 讨论(0)
  • 2021-02-05 14:16

    Try this polyfill which doesn't replace a native (and functional) CustomEvent method.

    (function () {
      try {
        new CustomEvent('test');
        return;
      } catch(e) {
        // ignore this error and continue below
      }
    
      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.Event.prototype;
      window.CustomEvent = CustomEvent;
    })();
    
    0 讨论(0)
  • 2021-02-05 14:23

    Afaik custom events are not supported in IE, only in normal browsers. I suggest using a javascript library that provides a browser independent implementation like Jquery's trigger: http://api.jquery.com/trigger/

    0 讨论(0)
  • 2021-02-05 14:24

    The following polyfill would not replacce native CustomEvent(),
    Partial source from: MDN CustomEvent():

    (function () {
    
      if (typeof CustomEvent === 'function') { return; }
    
      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.Event.prototype;
      window.CustomEvent = customEvent;
    })();
    
    0 讨论(0)
提交回复
热议问题