admin管理员组文章数量:1194745
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.
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.
The very minimal test is here:
I don't think this is a bug, though... so the question is: how do I get the error details in recent Firefox?
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.
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.
The very minimal test is here: 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?
Share Improve this question edited May 18, 2013 at 22:35 Sk8erPeter 6,9879 gold badges50 silver badges67 bronze badges asked Oct 5, 2012 at 12:13 ClaudioClaudio 5,9805 gold badges36 silver badges42 bronze badges1 Answer
Reset to default 25The HTML5 specification requires that a parse failure causes the browser to:
...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 message be a user-agent-defined string describing the error in a helpful manner.
...
Let event be 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 event at target.
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.
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
:
// 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; }
本文标签: javascriptHow to get error event details in Firefox using addEventListenerStack Overflow
版权声明:本文标题:javascript - How to get error event details in Firefox using addEventListener? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738507827a2090632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论