Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I override window.onerror in javascript should I return true or false?

I want to log JavaScript errors so I'm overriding window.onerror like this:

window.onerror = function(message, file, lineNumber) {     var browser_ = encodeURI(navigator.appVersion);     var error_ = encodeURI("msg:"+ message + "\n\tfile:"+file+"\n\tln:"+lineNumber);     var user_ = encodeURI("");      ...      return false; } 

I've seen some people return true and some return false. Which is right and why? One post mentioned something about have you have to return true or Firefox will handle the error it's own way. What??

like image 412
tooshel Avatar asked Nov 10 '11 22:11

tooshel


People also ask

What is the use of JavaScript Onerror event?

Definition and Usage The onerror event is triggered if an error occurs while loading an external file (e.g. a document or an image). Tip: When used on audio/video media, related events that occurs when there is some kind of disturbance to the media loading process, are: onabort.

What are the three information provided by Onerror () method?

The onerror() method These are: Error message – It is the same message that the browser would display for the given error. URL – It is the file location where the error occurred. Line number – It is the line number in the given URL that caused the error.

What is window Onerror?

onerror is a special browser event that fires whenever an uncaught JavaScript error has been thrown. It's one of the easiest ways to log client-side errors and report them to your servers. It's also one of the major mechanisms by which Sentry's client JavaScript integration (raven-js) works.


1 Answers

From MDN on window.onerror:

When the function returns true, this prevents the firing of the default event handler.

See also chromium Issue 92062:

In Chrome, returning true from window.onerror allows the error to propagate, and returning false suppresses it.

This is the inverse of the behavior in Firefox and IE, where returning 'true' suppresses the error, but returning false propagates it.

Note: the issue mentioned above was fixed, behavior is now as mentioned on MDN for all browsers.

like image 150
Tomasz Nurkiewicz Avatar answered Sep 20 '22 06:09

Tomasz Nurkiewicz