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

Error event handler.

Description

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 and we are just showing it for completeness.

  • Use .trigger( "error" ) instead of .error().
  • Use .on( "error", handler ) instead of .error( handler ).

Syntax

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

Parameters

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

Return

A jQuery object.

.error( handler(eventObject) ) Example  Events  <<  Top

Bind an event handler to the error JavaScript event.

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

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 'div1' 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 $('#curry1').error(addText) code.

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


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


$(function(){
  $('#curry1').error(addText);
  function addText(event) {
    $('#scrollspan1').append('error 1 **JavaScript event triggered** ');
  }
});

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

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

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

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

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: '#scrollspan2', 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: '#scrollspan2', 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" />


$(function(){
  $('#curry2').error({ param1: '#scrollspan2', param2: 'error 2 ', 
               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 - Browser & Loading Events