What is onerror() Method in JavaScript?


The onerror event handler was the first feature to facilitate error handling in JavaScript. The error event is fired on the window object whenever an exception occurs on the page.

Example

You can try to run the following code to implement onerror() method in JavaScript −

<html>
   <head>
      <script>
         <!--
            window.onerror = function () {
               alert("An error occurred.");
            }
         //-->
      </script>
   </head>
   <body>
      <p>Click the following to see the result:</p>

      <form>
         <input type = "button" value = "Click Me" onclick = "myFunc();" />
      </form>
   </body>
</html>

Updated on: 19-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements