GlobalEventHandlers.onerror

  • Revision slug: DOM/window.onerror
  • Revision title: window.onerror
  • Revision id: 286931
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment link to [[en/DOM/DOM_event_reference/error]]; 27 words added, 3 words removed

Revision Content

{{ DomRef() }}

Summary

An event handler for runtime script errors.

Note that some/many {{ domevent("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{{ mediawiki.external(0) }} through arguments{{ mediawiki.external(2) }}.

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

Specification

JavaScript 1.1.

Revision Source

<p>{{ DomRef() }}</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>An event handler for runtime script errors.</p>
<p>Note that some/many {{ domevent("error") }} events do not trigger <code>window.onerror</code>, you have to listen for them specifically.</p>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<pre>window.onerror = funcRef;
</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<ul> <li><code>funcRef</code> is a reference to a function. When the function returns <code>true</code>, this prevents the firing of the default event handler. Function parameters: <ul> <li>Error message (string)</li> <li>Url where error was raised (string)</li> <li>Line number where error was raised (number)</li> </ul> </li>
</ul>
<h3 id="Example" name="Example">Examples</h3>
<pre>// 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;
}
</pre>
<h3 id="Notes" name="Notes">Notes</h3>
<p>The error event is raised when an error occurs in the script.</p>
<p>When using the inline html markup (<span class="nowiki">&lt;body onerror="alert('an error occurred')&gt;...</span>), the arguments are not named. They can be referenced by <code>arguments{{ mediawiki.external(0) }}</code> through <code>arguments{{ mediawiki.external(2) }}</code>.</p>
<p>There is no <code><a class="external" href="/en/Components.stack" title="https://developer.mozilla.org/En/Components.stack">Components.stack</a>.caller</code> to retrieve. (See <a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=355430"><strong>bug 355430</strong></a>.)</p>
<h3 id="Specification" name="Specification">Specification</h3>
<p><a class="external" href="http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.3/reference/handlers.html#1120097" title="http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.3/reference/handlers.html#1120097">JavaScript 1.1</a>.</p>
Revert to this revision