mozilla

GlobalEventHandlers.onerror

An event handler for runtime script errors.

Note that some/many error events do not trigger window.onerror, you have to listen for them specifically.

Syntax

window.onerror = funcRef;

Parameters

  • funcRef is a reference to a function. When the function returns true, this prevents the firing of the default event handler. Function parameters:
    • Error message (string)
    • Url where error was raised (string)
    • Line number where error was raised (number)
    • Column number for the line where the error occurred (number) Requires Gecko 31.0
    • Error Object (object) Requires Gecko 31.0

Examples

// 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;
}

Notes

The error event is raised when an error occurs in the script.

When using the inline html markup (<body onerror="alert('an error occurred')>...), the arguments are not named. They can be referenced by arguments[0] through arguments[2].

There is no Components.stack.caller to retrieve. (See bug 355430.)

Specifications

JavaScript 1.1

Document Tags and Contributors

Last updated by: robatron,
Hide Sidebar