javascript - 将 Javascript 语法错误和 console.log 重定向到其他地方

标签 javascript redirect syntax console

我正在尝试将通常出现在 javascript 控制台中的任何内容发送到我自己的自定义函数。

我试过以下方法

window.console.error = window.console.debug =  window.console.log
window.console.log=mylog;

function mylog(msg){
    //send it somewhere just using alert as an example
      alert("log="+msg);    
}
console.log("yo");
var x=y;//a syntax error

使用上面的代码我只看到以下警告 “哟”

在控制台日志中我看到 “x 未定义”
如何重定向语法错误?

最佳答案

您可以使用 onerror 它旨在捕获语法错误。

window.onerror = function(message, source, lineno, colno, error) {
   alert(message);
}

Offical docs on MDN

关于javascript - 将 Javascript 语法错误和 console.log 重定向到其他地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35759269/

相关文章:

javascript - 幻灯片:最后一张图片闪烁页面长度?

javascript - Angular html 文件找不到 JS Controller [Node.js/Angular]

javascript - 在Reactcropperjs中将base64转换为文件类型?

apache - 将旧子域重定向到新子域

C结构语法

vim - vim 中类型推断的 ctags 跳转可能吗?

Jquery 选择器 + 语法

javascript - 使用 noUiSlider 以与显示格式不同的格式更新隐藏输入

redirect - 从 seo 的角度来看,页面重定向到最新文章是否不好?

java - 如何重定向到库中的 Controller ?