anonymous-one
anonymous-one

Reputation: 15112

Inconsistent window.onerror return true / false between browsers - any list of correct return values available?

I understand there is some inconstancy between what window.onerror should return if we do not want to invoke the browsers javascript error handler.

Since our site is fairly ajax heavy, we need to make sure the browsers error handler is not invoked ever.

What I mean by this:

window.onerror=function(){
    console.log('error captured');
    return false;
}

The "return false" part, apparently, differs browser to browser.

Does anyone know of somewhere we can find a list of how this is handled browser to browser, something a-la quirksmode / caniuse?

Or if the list is fairly small, does anyone know the values needed to be returned to suppress each browsers error handler off the top of their head?

Thanks!

Upvotes: 2

Views: 1774

Answers (1)

Thom Porter
Thom Porter

Reputation: 2609

You have your work cut out for you.

First off, you return true from your onerror handler, not false... Think of it as the browser is asking you if it should stop.

Next, there are some versions of some browsers that just tank in this area. They just don't work as advertised sometimes, no one seems to know why. The most intelligent answer I've ever seen is about call stack sizes getting too big... it sounded good to me!

That said, this should do the trick:

<script>
function stopErrors() {
    return false;
}
window.onerror = stopErrors;
</script>

Some key points from http://api.jquery.com/error/:

Note: A jQuery error event handler should not be attached to the window object. The browser fires the window's error event when a script error occurs. However, the window error event receives different arguments and has different return value requirements than conventional event handlers. Use window.onerror instead.

and

The event handler must be attached before the browser fires the error event ... Also, the error event may not be correctly fired when the page is served locally; error relies on HTTP status codes and will generally not be triggered if the URL uses the file: protocol.

Upvotes: 1

Related Questions