HTML - attributes - onerror

revision:


Content

"onerror" attribute : error during loading of media file syntax some examples


"onerror" attribute : error during loading of media file

top

This attribute fires when an error occurred during the loading of a media file.
The "onerror attribute" is part of the event attributes and can be used on the following elements:<audio>, <body>, <embed>, <img>, <link>, <object>, <script>, <video>.


syntax

top

<element onerror="script"></element >

value: a function which serves as the event handler for the error event. When an error occurs, the specified function will be called. If null, no error handler is in effect.


some examples

top

A function is triggered if an error occurs when loading the image. The function shows an alert box with a text. In this example we refer to an image that does not exist, therefore the onerror event occurs.

codes:
                    <img style="margin-left:3vw;" src="../../pics/image.gif" onerror="errorFunction()">
                    <script>
                        function errorFunction() {
                            document.getElementById("demo_error").innerHTML = "The image could not be loaded!!";
                        }
                    </script>
                

codes:
                    <img style="margin-left:3vw;"  src="../../pics/image.gif" onerror=someFun()>
                    <P class="spec" id="fun" style="font-size: 2vw; color:red;"></P>
                    <script>
                        function someFun() {
                            document.getElementById('fun').innerHTML = 'Error!!';
                        }
                    </script>