如果我在 javascript 中覆盖 window.onerror 我应该返回 true 还是 false?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8087240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:13:42  来源:igfitidea点击:

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

javascripterror-handling

提问by tooshel

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

我想记录 JavaScript 错误,所以我window.onerror像这样覆盖:

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 trueand 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??

我见过一些人回来true,一些人回来false。哪个是正确的,为什么?一篇文章提到了一些关于你必须返回 true 或者 Firefox 会以自己的方式处理错误的内容。什么??

回答by Tomasz Nurkiewicz

From MDNon window.onerror:

MDN开始window.onerror

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

当函数返回时true,这会阻止触发默认事件处理程序。

See also chromium Issue 92062:

另见铬问题 92062

In Chrome, returning truefrom window.onerror allows the error to propagate, and returning falsesuppresses it.

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

在 Chrome 中,true从 window.onerror返回允许错误传播,并返回false抑制它。

这与 Firefox 和 IE 中的行为相反,返回 'true' 会抑制错误,但返回会false传播错误。

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

注意:上述问题已修复,现在所有浏览器的行为都如 MDN 上所述。