javascript - If EventConstructor is not a constructor, how should I create the event? - Stack Overflow

I'm following MDN guide and trying to create an event:MDN guide for creating eventsvar jGp = new O

I'm following MDN guide and trying to create an event:

MDN guide for creating events

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

The error is (Safari):

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

Also to use document.createEvent seams to be deprecated (as MDN says), so how should I create my event?

I'm following MDN guide and trying to create an event:

MDN guide for creating events

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

The error is (Safari):

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

Also to use document.createEvent seams to be deprecated (as MDN says), so how should I create my event?

Share Improve this question edited Mar 21, 2014 at 23:31 Aditya Peshave 6671 gold badge9 silver badges26 bronze badges asked Mar 21, 2014 at 23:24 GustavoGustavo 1,6834 gold badges24 silver badges41 bronze badges 2
  • What version f Safari are you using and what platform are you on? It works fine for me. – kinakuta Commented Mar 21, 2014 at 23:30
  • Safari 5.1.10 (Snow Leopard). – Gustavo Commented Mar 22, 2014 at 1:09
Add a ment  | 

1 Answer 1

Reset to default 7

I ran into this too on Safari, I used a try/catch statement in order to use the non-deprecated constructor whenever possible, but fail into the old way 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);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744893791a4599573.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信