如果我在 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
If I override window.onerror in javascript should I return true or false?
提问by tooshel
I want to log JavaScript errors so I'm overriding window.onerror
like 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 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??
我见过一些人回来true
,一些人回来false
。哪个是正确的,为什么?一篇文章提到了一些关于你必须返回 true 或者 Firefox 会以自己的方式处理错误的内容。什么??
回答by Tomasz Nurkiewicz
From MDNon 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
true
from window.onerror allows the error to propagate, and returningfalse
suppresses it.
This is the inverse of the behavior in Firefox and IE, where returning 'true' suppresses the error, but returningfalse
propagates 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 上所述。