自动反馈JavaScript错误 [英] Automatic feedback on JavaScript error
本文介绍了自动反馈JavaScript错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果在客户端运行JavaScript时发生错误(甚至语法错误),是否有自动反馈的方法?
Is there a way to get an automatic feedback if an error (even syntax errors) occurs when running a JavaScript on the client side?
我在想像这样:
<script src="debugger.js"></script>
<script>
// some script with an error in it
</script>
每次调试器注意到错误,它会向服务器发送反馈。
And every time the debugger notices an error it sends a feedback to the server.
推荐答案
编辑:我最初误解了你的问题,这应该是有效的:
I misunderstood your question initially, this should work:
这需要在任何可能导致错误的JavaScript之前去。
Also note, this needs to go BEFORE any javascript that might cause errors.
window.onerror = function(msg,u,l)
{
txt ="Error: " + msg + "\n";
txt+="URL: " + u + "\n";
txt+="Line: " + l + "\n\n";
//Insert AJAX call that passes data (txt) to server-side script
return true;
};
对于语法错误,你没有运气。当有语法错误时,Javascript就会死机。没有办法处理它。
As for syntax errors, you're out of luck. Javascript simply dies when there's a syntax error. There's no way to handle it at all.
这篇关于自动反馈JavaScript错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文