jQuery API

.error()

.error( handler(eventObject) ) Returns: jQuery

Description: Bind an event handler to the "error" JavaScript event.

  • version added: 1.0.error( handler(eventObject) )

    handler(eventObject)A function to execute when the event is triggered.

  • version added: 1.4.3.error( [eventData], handler(eventObject) )

    eventDataA map of data that will be passed to the event handler.

    handler(eventObject)A function to execute each time the event is triggered.

This method is a shortcut for .bind('error', handler).

The error event is sent to elements, such as images, that are referenced by a document and loaded by the browser. It is called if the element was not loaded correctly.

For example, consider a page with a simple image element:

<img alt="Book" id="book" />

The event handler can be bound to the image:

$('#book')
  .error(function() {
    alert('Handler for .error() called.')
  })
  .attr("src", "missing.png");

If the image cannot be loaded (for example, because it is not present at the supplied URL), the alert is displayed:

Handler for .error() called.

The event handler must be attached before the browser fires the error event, which is why the example sets the src attribute after attaching the handler. Also, the error event may not be correctly fired when the page is served locally; error relies on HTTP status codes and will generally not be triggered if the URL uses the file: protocol.

Note: A jQuery error event handler should not be attached to the window object. The browser fires the window's error event when a script error occurs. However, the window error event receives different arguments and has different return value requirements than conventional event handlers. Use window.onerror instead.

Example:

To hide the "broken image" icons for IE users, you can try:

$("img")
  .error(function(){
    $(this).hide();
  })
  .attr("src", "missing.png");

Support and Contributions

Need help with .error() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .error()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://www.barelyfitz.com/ BarelyFitz

    A couple notes about .error():

    If you use it on img element, and set the src to be a non-image url (like attempting to check if an HTML page exists), the error handler will trigger even if the url is successfully loaded.

    Also when using on an img element, you should bind the error handler before you set the src of the image, or it might not trigger correctly.

    Using .error() on an iframe never seems to trigger (even though .load() will trigger on a successful iframe load).

  • Anonymous

    this example

    $(window).error(function(msg, url, line){
    jQuery.post(“js_error_log.php”, { msg: msg, url: url, line: line });
    });

    does not work in 1.4.2

    the handler is passed the event object like all other event handlers but the additional arguments [msg, url, line] are not given.

  • xfinx

    It would be really nice if the additional arguments are given again. Any idea when or if we can expect this again?

  • http://deadlyicon.com/ Jared Grippe

    no idea. I’m not even sure if a bug has been filed yet. =

  • http://openid-provider.appspot.com/fbogner fbogner

    Some more note about .error()

    It uses the onerror callback. This callback is only defined for img tags. Some browsers (IE,FF,…) support this callback on other objects too. This is great for a global error catching system (window.onerror). This behaviour is NOT conform to standards. WebKit for example does not fire this callback!

  • Xyzy2

    .error() event will not trigger when the src attribute is empty.

  • Guy

    This call does not handle 403 responses on FF.

  • Whitewizard

    i use jquery 1.4.2 and jquery.getJSON. the server response invalid json. so i try to use this example

    this example
    $(window).error(function(){
    return true;
    });

    but it's not work. the alert dialog still appear. please let me know how to do.

  • Whitewizard

    i use jquery 1.4.2 and jquery.getJSON. the server response invalid json. so i try to use this example

    this example
    $(window).error(function(){
    return true;
    });

    but it's not work. the alert dialog still appear. please let me know how to do.

  • Fawad

    Is there anything that works so I can handle javascript errors? my users are not javascript programmer why should they get “debug this page message box,” jquery should have done something here as well

  • Ahmed safan

    its a great problem that error event for an image element is fired multiple times about 3 times more or minus depending on browser and i should detect the last one for my action that is changing the src of the img.
    when i tried to change the src it has gone to infiniti loop of firing error
    those images paths exists and works fine if not set from error event