JavaScript Tutorial/Window/onerror

Материал из Web эксперт
Перейти к: навигация, поиск

Error occurs before the onerror event handler (Error will not be displayed)

   <source lang="javascript">

<html>

   <head>
       <title>OnError Example</title>
       <script type="text/javascript">
           alert("Syntax error.";
           window.onerror = function (sMessage, sUrl, sLine) {
               alert("An error occurred:\n" + sMessage + "\nURL: " + sUrl + "\nLine Number: " + sLine);
               return true;
           }
       </script>
   </head>
   <body onload="nonExistentFunction()">
   </body>

</html></source>


Get error file url

   <source lang="javascript">

<html>

   <head>
       <title>OnError Example</title>
       <script type="text/javascript">
           window.onerror = function (sMessage, sUrl, sLine) {
               alert("An error occurred " + "\nURL: " + sUrl);
               return true;
           }
       </script>
   </head>
   <body onload="nonExistentFunction()">
   </body>

</html></source>


Get error line number

   <source lang="javascript">

<html>

   <head>
       <title>OnError Example</title>
       <script type="text/javascript">
           window.onerror = function (sMessage, sUrl, sLine) {
               alert("An error occurred " + "\nLine Number: " + sLine);
               return true;
           }
       </script>
   </head>
   <body onload="nonExistentFunction()">
   </body>

</html></source>


Get error message

   <source lang="javascript">

<html>

   <head>
       <title>OnError Example</title>
       <script type="text/javascript">
           window.onerror = function (sMessage, sUrl, sLine) {
               alert("An error occurred:\n" + sMessage);
               return true;
           }
       </script>
   </head>
   <body onload="nonExistentFunction()">
   </body>

</html></source>


Return true from window onerror event

   <source lang="javascript">

<html>

   <head>
       <title>OnError Example</title>
       <script type="text/javascript">
           window.onerror = function () {
               alert("An error occurred.");
               return true;
           }
       </script>
   </head>
   <body onload="nonExistentFunction()">
   </body>

</html></source>


Window on error event

   <source lang="javascript">

<html>

   <head>
       <title>OnError Example</title>
       <script type="text/javascript">
           window.onerror = function () {
               alert("An error occurred.");
           }
       </script>
   </head>
   <body onload="nonExistentFunction()">
   </body>

</html></source>