15

Duplicate:


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

1 Answer 1

27

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Heh... Just linked to a previous question where you gave nearly the same answer. You're really on the ball when it comes to JS error logging! :-)
lol... It's funny you caught that, and I don't even remember answering that question.
@George Jempty: Read my explanation. I gave jQuery because it's fairly pervasive and much more succinct to type out. The example is valid as it's simply a model. I didn't give the source code for my php script or the layout for the MySQL table storing the errors either.
Ideally I wanted a jQuery answer but I didn't want to restrict the answer to that. Thanks
I have just released code to help log JavaScript errors by sending error information to the server - thecodepage.com/post/JavaScript-Error-Notifications.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.