javascript 如何使用 addEventListener 在 Firefox 中获取错误事件详细信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12746034/
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
How to get error event details in Firefox using addEventListener?
提问by Claudio
I'm trying to understand why Firefox (I'm using 15 but it's the same even in nightly) is not behaving like WebKit when trying to access error event information.
我试图理解为什么 Firefox(我使用的是 15,但即使在夜间也一样)在尝试访问错误事件信息时表现得不像 WebKit。
This one works everywhere:
这个适用于任何地方:
window.onerror = function(message, lineno, filename) { }
But of course I don't want to use this.
但我当然不想使用这个。
The right thing to do is:
正确的做法是:
window.addEventListener('error', function(e) {
console.log(e.message);
}, false);
Unfortunately this one only works in WebKit. In Firefox the handler is called, but the e
event is almost empty: no message, no line number, no filename properties.
不幸的是,这仅适用于 WebKit。在 Firefox 中,处理程序被调用,但e
事件几乎是空的:没有消息,没有行号,没有文件名属性。
The very minimal test is here: http://jsbin.com/efexiw/1/edit
最简单的测试在这里:http: //jsbin.com/efexiw/1/edit
I don't think this is a bug, though... so the question is: how do I get the error details in recent Firefox?
不过,我不认为这是一个错误……所以问题是:如何在最近的 Firefox 中获取错误详细信息?
回答by apsillers
The HTML5 specification requiresthat a parse failure causes the browser to:
在HTML5规范要求,一个解析失败会导致浏览器:
...report the error for script, with the problematic position (line number and column number), using the global object... as the target.
...报告脚本的错误,有问题的位置(行号和列号),使用全局对象...作为目标。
Where "report the error" includes the steps
其中“报告错误”包括步骤
- Let messagebe a user-agent-defined string describing the error in a helpful manner.
...
Let eventbe a new trusted
ErrorEvent
object that does not bubble but is cancelable, and which has the event nameerror
.Initialize event's
message
attribute to message....
- Dispatch eventat target.
- 让message是一个用户代理定义的字符串,以一种有用的方式描述错误。
...
让event成为一个新的可信
ErrorEvent
对象,它不会冒泡但可以取消,并且具有事件名称error
。将事件的
message
属性初始化为消息。...
- 在目标处调度事件。
Thus, any HTML5-compliant browser will report parse-time error events on window
, which include a message
attribute set to a "user-agent-defined string describing the error in a helpful manner." Any browser version that fails to do this is not yet HTML5 compliant in this regard.
因此,任何符合 HTML5 的浏览器都会在 上报告解析时错误事件window
,其中包括一个message
属性设置为“用户代理定义的以有用的方式描述错误的字符串” 。任何未能做到这一点的浏览器版本还没有在这方面符合 HTML5。
Previously (at the time this question was written), window.onerror
gave information that was not provided by window.addEventListener("error")
. If you must use an old version of Firefox, you can safely use window.onerror
:
以前(在撰写此问题时),window.onerror
提供了window.addEventListener("error")
. 如果您必须使用旧版本的 Firefox,您可以安全地使用window.onerror
:
// Example 1: // Prevent error dialogs from displaying -which is the window's normal // behavior- by overriding the default event handler for error events that // go to the window. window.onerror = null; // Example 2: var gOldOnError = window.onerror; // Override previous handler. window.onerror = function myErrorHandler(errorMsg, url, lineNumber) { if (gOldOnError) // Call previous handler. return gOldOnError(errorMsg, url, lineNumber); // Just let default handler run. return false; }
// Example 1: // Prevent error dialogs from displaying -which is the window's normal // behavior- by overriding the default event handler for error events that // go to the window. window.onerror = null; // Example 2: var gOldOnError = window.onerror; // Override previous handler. window.onerror = function myErrorHandler(errorMsg, url, lineNumber) { if (gOldOnError) // Call previous handler. return gOldOnError(errorMsg, url, lineNumber); // Just let default handler run. return false; }