如何使用addEventListener在Firefox中获取错误事件详细信息? [英] How to get error event details in Firefox using addEventListener?

查看:157
本文介绍了如何使用addEventListener在Firefox中获取错误事件详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在尝试访问错误事件信息时,我试图理解为什么Firefox(我使用的是15,但即使在夜晚也是如此)不像WebKit那样行事。

  window.onerror =函数(message,lineno,filename){} 

但当然我不想用这个。



正确的做法是:
$ b $ pre $ window.addEventListener('error',function(e){
console.log(e.message);
},false);

不幸的是,这个只适用于WebKit。在Firefox中,处理程序被调用,但是 e 事件几乎为空:没有消息,没有行号,没有文件名属性。



最小的测试在这里: http://jsbin.com/efexiw/1/edit

我不认为这是一个错误,但是...所以问题是:如何获取最近的Firefox中的错误细节?

解决方案

HTML5规范要求,解析失败导致浏览器:


...报告脚本错误,与问题的位置(行号和列号),使用全局对象...作为目标。

其中报告错误包含步骤



  1. message 成为用户代理定义的字符串,以有用的方式描述错误。 $ b

...


  1. event 成为一个新的可信的 ErrorEvent 对象,它不会冒泡但是可以取消,而且事件名称 error

  2. 初始化事件消息属性为消息


...
$
$ b

  • 因此,任何符合HTML5的浏览器都会在窗口上报告解析错误事件,这些事件包括:
    <一个消息属性被设置为用户代理定义的字符串,以有帮助的方式描述错误。任何浏览器版本都无法做到这一点在这方面还不符合HTML5。





    以前(写这个问题的时候), window.onerror 给出了没有提供的信息。window.addEventListener ( 错误)。如果您必须使用旧版本的Firefox,则可以安全使用 window.onerror


      //例子1:

    //防止显示错误对话框 - 这是窗口的正常
    //行为 - 通过覆盖缺省事件处理程序的错误事件
    //去窗口
    window.onerror = null;

    //例子2:

    var gOldOnError = window.onerror;
    //覆盖以前的处理程序。
    window.onerror = function myErrorHandler(errorMsg,url,lineNumber){
    if(gOldOnError)
    //调用上一个处理程序。
    返回gOldOnError(errorMsg,url,lineNumber);

    //让默认处理程序运行。
    返回false;
    }



    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?

    解决方案

    The 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

    1. Let message be a user-agent-defined string describing the error in a helpful manner.

    ...

    1. Let event be a new trusted ErrorEvent object that does not bubble but is cancelable, and which has the event name error.

    2. Initialize event's message attribute to message.

    ...

    1. 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;
    }
    

    这篇关于如何使用addEventListener在Firefox中获取错误事件详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆