Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly initialize an ErrorEvent in javascript?

I need to fire an ErrorEvent programmatically, but can't figure out how to initialize the properties of the event. The properties are readonly, and initEvent() only initializes the event type, whether it bubbles, and if it's cancellable.

I tried

var myErrorEvent = new ErrorEvent("error", new Error("This is my error"));

and

myErrorEvent.initEvent("error", false, true, new Error("This is my error"));

or

// This function would be defined in IE only. Could not find it in Chrome.
myErrorEvent.initErrorEvent("error", false, true, "This is my error", "myfile.js", 1234);

I tried many other things, but could not get the properties of the ErrorEvent to be set properly.

Is this even feasible?

like image 455
sboisse Avatar asked Oct 24 '14 19:10

sboisse


1 Answers

You were close! The 2nd argument to the ErrorEvent constructor is an object with the properties you want to set, like this:

var error = new ErrorEvent('oh nose', {
    error : new Error('AAAHHHH'),
    message : 'A monkey is throwing bananas at me!',
    lineno : 402,
    filename : 'closet.html'
});

For the full set of properties, see the spec.

like image 157
Zirak Avatar answered Oct 09 '22 18:10

Zirak