如何捕获JavaScript异常/错误? (要在服务器上登录) [英] How to catch javascript exceptions/errors? (To log them on server)

查看:125
本文介绍了如何捕获JavaScript异常/错误? (要在服务器上登录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • Automatic feedback on JavaScript error
  • Logging JavaScript-Errors on Server






如何在javascript中记录错误?我不能在try catch块中包含每一行javascript。


How would I go about logging errors in javascript? I can't wrap every line of javascript in try catch block.

我在说到IE中的错误,会显示错误页面消息,并且行和char引起的错误。如果我只是想知道如何在客户端捕获这个错误,我可以使用ajax调用在服务器上记录错误。

I talking about the errors that for example in IE, would show an Error On page message and have the line and char the caused the error. If I can just figure out how to catch this error on the client side, I can just log the error on the server using an ajax call.

推荐答案

我在我所有的项目中使用这个功能:

I use this function in all my projects:

window.onerror = function(m,u,l){
    jQuery.post("ajax/js_error_log.php",
        { msg: m,
          url: u,
         line: l,
       window: window.location.href });
    return true};

确保它是浏览器接收到的第一个javascript,或至少在任何潜在的导致错误的代码之前。当然需要jQuery,但是如果你想要的话,你可以在纯JavaScript中编写ajax函数。

Make sure it is the very first javascript the browser receives or at least precedes any potentially error-causing code. Requires jQuery of course, but you could code ajax functions in pure javascript if you wanted to.

请注意:如果您有语法错误,这将无法帮助您。如果有语法错误,所有的javascript会立即死亡。

Please note: this will not help you if you have a syntax error. All javascript instantly dies if there is a syntax error.

这篇关于如何捕获JavaScript异常/错误? (要在服务器上登录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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