JavaScript Tutorial/Window/onerror

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

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

<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>


Get error file url

<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>


Get error line number

<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>


Get error message

<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>


Return true from window onerror event

<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>


Window on error event

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