views:

678

answers:

5

Is there a way to get an automatic feedback if an error (even syntax errors) occurs when running a JavaScript on the client side?

I was thinking of something like this:

<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.

+10  A: 

EDIT: I misunderstood your question initially, this should work:

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

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.

Andrew
I’ve thought of that too. But it seems that just Firefox does this. Safari and Opera both keep quiet when I try to alert the message.
Gumbo
Ok, perhaps it’s just alert that doesn’t work. Printing the error message does. I think that’s it. Thanks.
Gumbo
Works in FF. To get it to work in IE you have to disable script debugging in the advanced tab of internet options. I haven't got it to work in Chrome nor Opera.
some
You can also use try/catch blocks and report errors in the catch block.
Josh
I have just released code to help log JavaScript errors by sending error information to the server - http://www.thecodepage.com/post/JavaScript-Error-Notifications.aspx
Gabriel McAdams
+1  A: 

I'm not sure about syntax errors but it is possible to catch js errors using window.onerror event. Searching for "logging errors in javascript" in google gives you a lot of results...

Logging JavaScript Errors To ASP.NET: http://james.newtonking.com/archive/2006/05/02/Logging-JavaScript-Errors-To-ASP.NET.aspx

Logging Client Side JavaScript Errors to the Server: http://www.codeproject.com/KB/ajax/logclientsidejserrors2srv.aspx

Alex Reitbort
A: 

window.onerror = MyFunction(msg,url,line);

we pop up a window with the error details, browser type (i.e. navigator.userAgent), etc. all in fields marked READONLY and inviting the user to Submit them.

We have a checkbox "Don't show this again" which is stored in a session cookie - so if they keep getting errors than can disable the Popup.

Despite the fact that we thought this was "well cool", we get very few reports - and I'm not convinced that that is because we get close to zero runtime errors!

Kristen
+5  A: 

One technique is to use Ajax for Javascript error logging

Every javascript error can be trapped in the window.onerror event. We can return true or false so we can choose if the user shall see the normal javascript error dialog. This script will, in the very unlikely event of a javascript error, gather information about the error and send a httprequest to a page which will log the javascript error to the database.

Gulzar
I was looking for that page.. +1
Luca Matteis
Thanks for your link to this helpful function. I’ll try it.
Gumbo
A: 

Probably best to send only one error report per page. If you get an error on a repeating timer, or the AJAX itself recursively calls an error, you can end up filling your log with useless junk.

Also, be prepared to receive unsolvable errors caused by things like:

  • security proxies diddling with your code
  • scripts from blocked sites
  • unimportant browser chrome errors from extensions
  • IE Mobile, the worst browser in the world
bobince
ROFL @ IE Mobile
Andrew