How to get error event details in Firefox using addEventListener?

前端 未结 1 467
醉酒成梦
醉酒成梦 2020-12-08 11:53

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.

T

相关标签:
1条回答
  • 2020-12-08 12:04

    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;
    }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题