You are here: Reference > JavaScript > client-side > event handling > events > onerror

onerror event | error event

Browser support:
Fires when an error occurs while loading an external file.

How to register:

In HTML:
<ELEMENT onerror="handler">

In JavaScript:
object.onerror = handler;
object.addEventListener ("error", handler, useCapture);
9
object.attachEvent ("onerror", handler);
You can find the related objects in the Supported by objects section below.
The event object is accessible to all event handlers in all browsers. The properties of the event object contain additional information about the current event. To get further details about these properties and the possible event handler registration methods, please see the page for the event object.
For a complete list of events, see the page for Events in JavaScript.

Basic information:

Bubbles No
Cancelable Yes
Event object Event

Actions that invoke the onerror event:

  • When an error occurs during loading an external file.

Example HTML code 1:

This example illustrates the use of the onerror event on image elements:
<head>
    <script type="text/javascript">
        function ErrorHandler () {
            alert ("Cannot load the image!");
        }
    </script>
</head>
<body>
    <img onerror="ErrorHandler ()" src="not_existing_image.jpg" />
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example only works in Internet Explorer. It shows how to detect when an external style file cannot be imported:
<head>
    <style onerror="ErrorHandler ()">
        @import url("notexisting.css");
    </style>
    <script type="text/javascript">
        function ErrorHandler () {
            alert ("Cannot load the style file!");
        }
    </script>
</head>
<body>
</body>
Did you find this example helpful? yes no

Example HTML code 3:

This example only works in Google Chrome and Safari (and in Opera from version 9.5 to 10.5). It shows how to detect when an external JavaScript file cannot be loaded:
<head>
    <script type="text/javascript">
        function ErrorHandler () {
            alert ("Cannot load the JavaScript file!");
        }
    </script>
    <script type="text/javascript" src="notexisting.js" onerror="ErrorHandler ()"></script>
</head>
<body>
</body>
Did you find this example helpful? yes no

Supported by objects:

External links:

User Contributed Comments

Post Content

Post Content