.error()    **REMOVED** JQ Home  <<  Events  <<  .error()

Error event handler.


The .error() method is used to bind an event handler to The JavaScript error event.

  • The error event is sent to elements that are referenced by a document and not loaded correctly by the browser.
  • The event handler for the error event must be attached prior to the browser firing of the error event.
  • Use window.onerror if you need to attach an event handler to the window object as browsers fire off the window error event when a script error occurs. The window error event has different arguments and return value requirements and should be used in this case.

This method was deprecated in jQuery 1.8 and removed in 3.0.


Signature Description
.error( [eventData], handler(eventObject) )Bind an event handler to the error JavaScript event, optionally passing an object of data.


Parameter Description Type
eventDataAn object of data to pass to the event handler.Object
handler( eventObject )A function to execute each time the event is triggered.Function


A jQuery object.

.error( [eventData], handler(eventObject) ) Example  Events  <<  Top

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

Bind an event handler to the error JavaScript event, optionally passing a map of data.

The example below no longer works from jQuery 3.0 onwards and is just shown as an example for people using earlier versions.

In the example below we show a new message under the 'div' element when the image of chicken masala cannot be found. We are pointing to an invalid Url as shown below, so the image will never load and the error JavaScript event occurs. This then fires off the $('#curry2').error({ param1: '#scrollspan3', param2: 'error ', param3: '**JavaScript event triggered** ' }, addText2); code.

What we are doing here is passing across the event object to the function addText2(event). The map we specify, in our case { param1: '#scrollspan3', param2: 'error ', param3: '**JavaScript event triggered** ' } gets tagged onto the event.data property.

We then access these parameters in the function via event.data.paramN and use the passed parameters for our appended data.

<img id="curry2"  src="badurl"  alt="a picture of curry" 
     title="Chicken Masala"  width="130"  height="100" />

  $('#curry2').error({ param1: '#scrollspan3', param2: 'error ', 
               param3: '**JavaScript event triggered**  ' }, addText2);
  function addText2(event) {
    $(event.data.param1).append(event.data.param2 + '<code>' + event.data.param3 + '</code>');

We will show a message here when the error JavaScript event fires.

Related Tutorials

jQuery Advanced Tutorials - Lesson 1 - Loading & Browser Events