How to catch javascript exceptions/errors? (To log them on server)

不想你离开。 提交于 2019-12-03 01:32:28

问题


Duplicate:

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

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

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.


回答1:


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};

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.

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



来源:https://stackoverflow.com/questions/779644/how-to-catch-javascript-exceptions-errors-to-log-them-on-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!