This article needs a technical review. How you can help.

An event handler for the error event. Error events are fired at various targets for different kinds of errors:

  • When a JavaScript runtime error (including syntax errors and exceptions thrown within handlers) occurs, an error event using interface ErrorEvent is fired at window and window.onerror() is invoked (as well as handlers attached by window.addEventListener (not only capturing)).
  • When a resource (such as an <img> or <script>) fails to load, an error event using interface Event is fired at the element, that initiated the load, and the onerror() handler on the element is invoked. These error events do not bubble up to window, but (at least in Firefox) can be handled with a single capturing window.addEventListener.

Installing a global error event handler is useful for automated collection of error reports.


For historical reasons, different arguments are passed to window.onerror and element.onerror handlers (as well as on error-type window.addEventListener handlers).


window.onerror = function(messageOrEvent, source, lineno, colno, error) { ... }

Function parameters:

  • message: error message (string or event object). Available as event (sic!) in HTML onerror="" handler and also as an event object when dispatching an Event to window in which case the other arguments will not be supplied (as opposed to ErrorEvent which does cause the full range of arguments to be supplied to window.onerror whereas the single error event object is supplied to window.addEventListener('error') handlers) .
  • source: URL of the script where the error was raised (string)
  • lineno: Line number where error was raised (number)
  • colno: Column number for the line where the error occurred (number)
  • error: Error Object (object)

When the function returns true, this prevents the firing of the default event handler.

element.onerror (and window.addEventListener('error'))

element.onerror = function(event) { ... }

element.onerror accepts a function with a single argument of type Event.


When a syntax(?) error occurs in a script, loaded from a different origin, the details of the syntax error are not reported to prevent leaking information (see bug 363897). Instead the error reported is simply "Script error." This behavior can be overriden in some browsers using the crossorigin attribute on <script> and having the server send the appropriate CORS HTTP response headers.  A workaround is to isolate "Script error." and handle it knowing that the error detail is only viewable in the browser console and not accessible via JavaScript.

window.onerror = function (msg, url, lineNo, columnNo, error) {
    var string = msg.toLowerCase();
    var substring = "script error";
    if (string.indexOf(substring) > -1){
        alert('Script Error: See Browser Console for Detail');
    } else {
        var message = [
            'Message: ' + msg,
            'URL: ' + url,
            'Line: ' + lineNo,
            'Column: ' + columnNo,
            'Error object: ' + JSON.stringify(error)
        ].join(' - ');


    return false;

When using the inline HTML markup (<body onerror="alert('an error occurred')">), the HTML specification requires arguments passed to onerror to be named event, source, lineno, colno, error. In browsers that have not implemented this requirement, they can still be obtained via arguments[0] through arguments[2].


Specification Status Comment
WHATWG HTML Living Standard
The definition of 'onerror' in that specification.
Living Standard  

Browser compatibility

Before Firefox 14 when a <script> failed to load, window.onerror was invoked with message "Error loading script". This was fixed in bug 737087, now scriptElement.onerror is invoked instead in such cases.

Since Firefox 31, the last 2 arguments (colno and error) were added, meaning you can access the stack trace of a script error from window.onerror via the provided Error object (bug 355430.)

See also

Browser Compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) ? ? ? ?
Feature Android Android Webview Chrome for Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile
Basic support ? ? ? ? ? ? ? ?

Document Tags and Contributors

 Last updated by: jpmedley,