javascript - 谷歌浏览器中 window.onerror 中的错误参数为空

标签 javascript google-chrome

我有导致 js 错误的 html 页面和它的全局处理程序。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
    <script type="text/javascript">
        $(function () {
            window.onerror = function (errorMsg, url, lineNumber) {
                alert('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber);
            }

            throw new Error("this is error");
        });
    </script>
</head>
<body>
    <div>Test page</div>
</body>
</html>

我收到警报:“错误:脚本错误。脚本:行:0”。如您所见,缺少错误信息。我如何才能在此处获取此信息,就像我可以在开发人员工具中看到错误一样。 chrome 的任何扩展都不合适。我必须在 javascript 中捕获他们的信息错误。

最佳答案

When a syntax(?) error occurs in a script, loaded from a different origin, the details of the syntax error are not reported to prevent leaking information (see bug 363897). Instead the error reported is simply "Script error." This behavior can be overriden in some browsers using the crossorigin attribute on and having the server send the appropriate CORS HTTP response headers. A workaround is to isolate "Script error." and handle it knowing that the error detail is only viewable in the browser console and not accessible via JavaScript.

我也遇到过。请参阅此处的注释部分:https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror

这意味着开发设置可能需要进行一些调整以避免此政策问题。我只是在这里猜测,所以不要认为这是理所当然的。

关于javascript - 谷歌浏览器中 window.onerror 中的错误参数为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45239134/

相关文章:

javascript - 谷歌饼图 : How to show values instead of percentage?

javascript - 在 Rx.Observable 中一次执行一个 Promise

google-chrome - 开发者工具被禁用

macos - CSS3 @font-face 在 Mac OS X Safari 5.x 或 Chrome 上不显示

javascript - 内容脚本中的动态脚本加载

json - Chrome 的开发工具将网络响应预览显示为字符串

javascript - Extjs 4创建工具栏项目宽度ajax请求

Javascript Promises 显式函数与内联函数

javascript - JSON 数组响应中的总和值

html - html5中如何更改自动日期选择器的显示格式