If the EventConstructor is not a constructor, how do I create an event?

I follow the guide MDNand try to create an event:

Event MDN Guide

var jGp = new Object();
...
jGp.evt = new Object();
jGp.evt.erro = new Event("jGp_evtErro");

Error (Safari):

TypeError: '[object EventConstructor]' is not a constructor (evaluating 'new Event("jGp_evtErro")')

Also, to use document.createEventstitches for obsolescence (as they say MDN), how do I create my event?

+4
source share
1 answer

I also came across this in Safari, I used the try / catch instruction to use the constructor without the obsolete ones, when possible, but do not execute the old one if necessary.

jGp.evt = new Object();
try {
  jGp.evt.erro = new Event("jGp_evtErro");
}
catch (e) {
  jGp.evt.erro = document.createEvent('Event');
  jGp.evt.erro.initEvent("jGp_evtErro", true, true);
}
+6
source

Source: https://habr.com/ru/post/1532927/


All Articles