我有导致 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/