GlobalEventHandlers.onerror

  • Revision slug: DOM/window.onerror
  • Revision title: window.onerror
  • Revision id: 37535
  • Created:
  • Creator: SergeG
  • Is current revision? No
  • Comment 5 words added, 8 words removed

Revision Content

{{ DomRef() }}

Summary

An event handler for error events sent to the window.

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

Not part of specification.

Revision Source

<p>{{ DomRef() }}</p>
<h3 name="Summary">Summary</h3>
<p>An event handler for error events sent to the window.</p>
<h3 name="Syntax">Syntax</h3>
<pre>window.onerror = funcRef;
</pre>
<h3 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 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 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 name="Specification">Specification</h3>
<p>Not part of specification.</p>
Revert to this revision