如何测试window.onerror处理程序?例如,如果我尝试throw 'Error2';,则不会触发该事件。什么事件处理程序将对throw 'Error2';做出反应,什么事件将触发window.onerror

comment for first answer

最佳答案

签入官方文档-https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror

<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Page Title</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <script>
        window.onerror = function (msg, url, lineNo, columnNo, error) {
            var string = msg.toLowerCase();
            var substring = "script error";
            if (string.indexOf(substring) > -1) {
                alert('Script Error: See Browser Console for Detail');
            } else {
                var message = [
                    'Message: ' + msg,
                    'URL: ' + url,
                    'Line: ' + lineNo,
                    'Column: ' + columnNo,
                    'Error object: ' + JSON.stringify(error)
                ].join(' - ');

                alert(message);
            }

            return false;
        };
        throw 'Error2';
    </script>
</head>

<body>

</body>

</html>

关于javascript - 如何测试window.onerror处理程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59320205/

10-12 13:04