window.onerror Redirect 1

« DOM Reference


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.


window.onerror = funcRef;


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


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


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


JavaScript 1.1

Document Tags and Contributors

 Last updated by: Sheppy,