GlobalEventHandlers.onerror

« DOM Reference

Summary

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)

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.)

Specification

JavaScript 1.1

Document Tags and Contributors

Contributors to this page: Sheppy, Delapouite, asassssssss, SergeG, Nickolay, Doekman, Rappo, kscarfone, teoli, Nux
Last updated by: Delapouite,