.error()    **DEPRECATED**S2C 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 is shown for completeness.

Syntax


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

Parameters


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

Return

A jQuery object.

.error( [eventData], handler(eventObject) ) Examplego to top of page 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.

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" />

$(function(){
  $('#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>');
  }
});

A picture of curry


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

go to home page Homepage go to top of page Top