Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Purpose of onerror event handler in javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 457
    Comment on it

    In JavaScript onerror is a Document Object Model event handler. It occurs when error occur during object loading. In JavaScript window.onerror is used as an event handler, there is no error event being triggered: Alternatively, when the exception remain uncaught or compile-time error, the window.onerror event handler is called with the information about the error.
    Uncaught exceptions

    • throw "your messages"
    • call_something_undefined()
    • cross_origin_iframe.contentWindow.document, a security exception


    Firstly the Scripts are compiled and then they get executed. If in the first time compile is successful, it is not compiled again further. Still, a script or a function may have have runtime errors that are compiled successfully , and we can use a function multiple times. Syntax

    object.onerror=function(){myScript};
    

    Here is an example to explain it more clearly

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>This example uses "onerror" event to an image element.</p>
    
    <img id="newimage" src="newimage.gif">
    
    <p id="demo"></p>
    
    <script>
    document.getElementById("newimage").onerror = function() {myFunction()};
    
    function myFunction() {
        document.getElementById("demo").innerHTML = "The image could not be loaded.";
    }
    </script>
    
    </body>
    </html>
    


    when this code is executed a popup come and display that the image cannot be loaded.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: