Javascript DOM onerror Event

Introduction

Execute a JavaScript if an error occurs when loading an image:

View in separate window

<!DOCTYPE html>
<html>
<body>

<img src="image.gif" onerror="myFunction()">

<p id="demo"></p>
<script>
function myFunction() {//from   ww  w  . java2 s  .  c  o m
  document.getElementById("demo").innerHTML = 'The image could not be loaded.';
}
</script>

</body>
</html>

The onerror event is triggered if an error occurs while loading an external file.

Bubbles: No
Cancelable: No
Event type: UiEvent if generated from a user interface, Event otherwise
Supported HTML tags: <img>, <input type="image">, <object>, <link>, and <script>



PreviousNext

Related