简体   繁体   English

将JavaScript控制台错误写入弹出窗口

[英]writing javascript console errors to a popup

I have the following code, my goal is to write to a pop up the console error message that results from calling the function "func". 我有以下代码,我的目标是写一个弹出的控制台错误消息,该错误消息是由调用函数“ func”导致的。 I am currently running it in chrome and although I get an error in the console I get no pop up. 我目前正在chrome中运行它,尽管在控制台中出现错误,但没有弹出窗口。 The reason I am doing this is that I will be running my code in another environment where I won't have access to the console so I would like to be able to extract the error messages so I can view them. 我这样做的原因是,我将在无法访问控制台的另一个环境中运行我的代码,因此我希望能够提取错误消息以便查看它们。

 function func(){ return y } (function() { var exLog = console.log; console.log = function(msg) { exLog.apply(this, arguments); alert(msg); } })() func() 

You can use window.onerror to log the errors 您可以使用window.onerror记录错误

window.onerror = function(msg) {
    alert(msg);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM