将Javascript语法错误和console.log重定向到其他地方 [英] redirect Javascript syntax errors and console.log to somewhere else

查看:64
本文介绍了将Javascript语法错误和console.log重定向到其他地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将通常会在javascript控制台中显示的所有内容发送到我自己的自定义函数.

I'm trying to send whatever would normally appear in the javascript console to my own custom functions.

我尝试了以下

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

使用上面的代码,我只会看到以下警报哟"

Using the code above I only see the following alert "yo"

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

In the console log I see "x is not defined"
How do I redirect the syntax errors?

推荐答案

您可以使用 onerror 来捕获语法错误.

You can use onerror it is designed to catch syntax errors.

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

有关MDN的官方文档

这篇关于将Javascript语法错误和console.log重定向到其他地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆