javascript:如何在弹出式警报中显示脚本错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2604976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:06:53  来源:igfitidea点击:

javascript: how to display script errors in a popup alert?

javascripterror-handling

提问by powerboy

I want to display script errors in a popup alert instead of showing them in the browser console.

我想在弹出式警报中显示脚本错误,而不是在浏览器控制台中显示它们。

window.onerror = function() {
  var message = /* get error messages and put them here */;
  alert(message);
  return true;
};

回答by DVK

Yes, that is the correct way.

是的,这是正确的方法。

See the reference here:

请参阅此处的参考:

http://www.javascriptkit.com/javatutors/error2.shtml

http://www.javascriptkit.com/javatutors/error2.shtml

And explanation of how to see more details of the error here:

以及如何在此处查看错误的更多详细信息的说明:

http://www.javascriptkit.com/javatutors/error3.shtml

http://www.javascriptkit.com/javatutors/error3.shtml

Their example:

他们的例子:

window.onerror = function(msg, url, linenumber) {
    alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
    return true;
}

If you wish to display a LIST of errors in a single pop-up, it's trickier.

如果您希望在单个弹出窗口中显示错误列表,则比较棘手。

Since the errors occue 1 by 1, you need to do the following:

由于错误是 1 个 1 个,因此您需要执行以下操作:

  • have window.onerrorhandler store error details in some array
  • Check that array periodically - either via a timer, or on every N'th call of window.onerrorhandler, or both.

    When the check happens, process entire array, display contents as desired, and empty out an array

  • window.onerror处理程序在某个数组中存储错误详细信息
  • 定期检查该数组 - 通过计时器,或在每次第 N 次调用window.onerror处理程序时,或两者兼而有之。

    检查发生时,处理整个数组,根据需要显示内容,并清空数组

回答by user1398498

Just in case someone would like to use it with jQuery:

以防万一有人想将它与 jQuery 一起使用:

$(window).on("error", function(evt) {

    console.log("jQuery error event:", evt);
    var e = evt.originalEvent; // get the javascript event
    console.log("original event:", e);
    if (e.message) { 
        alert("Error:\n\t" + e.message + "\nLine:\n\t" + e.lineno + "\nFile:\n\t" + e.filename);
    } else {
        alert("Error:\n\t" + e.type + "\nElement:\n\t" + (e.srcElement || e.target));
    }
});

回答by Bialecki

Check this out: http://www.javascriptkit.com/javatutors/error3.shtml. Looks like signature is function(message, url, linenumber).

看看这个:http: //www.javascriptkit.com/javatutors/error3.shtml。看起来签名是function(message, url, linenumber)

回答by user8188295

<script>$(window).on("error", function(evt) {

console.log("jQuery error event:", evt);
var e = evt.originalEvent; // get the javascript event
console.log("original event:", e);
if (e.message) { 
    alert("Error:\n\t" + e.message + "\nLine:\n\t" + e.lineno + "\nFile:\n\t" + e.filename);
} else {
    alert("Error:\n\t" + e.type + "\nElement:\n\t" + (e.srcElement || e.target));
}
});
</script>