views:

863

answers:

6

Hi

Im running a ASP.NET Site where I have problems to find some JavaScript-Errors just with manual testing.

Is there a possibility to catch all JavaScript-Errors on the Client Side and log them on the Server i.e. in the EventLog (via Webservice or something like that)?

Thanks for your answers!

+2  A: 

You could potentially make an Ajax call to the server from a try/catch, but that's probably about the best you can do.

May I suggest JavaScript unit testing instead? Possibly with JSUnit?

Mike Stone
The problem why we are not using JavaScript UnitTesting is because there are too many people contributing to the Site/Content and they are using JavaScript (but don't have a clue about it!) so a general solution would be better.
MADMap
A: 

The problem why we are not using JavaScript UnitTesting is because there are too many people contributing to the Site/Content and they are using JavaScript. The Content is nothing we (as developers) should care about, but there are mistakes in the code. So a general solution would be better.

MADMap
I assume you don't mean average user contributed (else that is XSS hole)... but... you could maybe isolate their JS in try/catch so it at least doesn't affect your own JS... without knowing the dynamic of the site, I don't know if this will help or not...
Mike Stone
The content is from other Teams in the Company, not from users so it's not an security-risk
MADMap
+10  A: 

You could try setting up your own handler for the onerror event and use XMLHttpRequest to tell the server what went wrong, however since it's not part of any specification, support is somewhat flaky.

Here's an example: Using XMLHttpRequest to log JavaScript errors

insin
I just released a server control that helps you do this at http://www.thecodepage.com/post/JavaScript-Error-Notifications.aspx
Gabriel McAdams
A: 

Also I recomend using TraceTool utility, it comes with JavaScript support and is very handy for JS monitoring.

cleg
A: 

If you're wanting to log the client-side errors back to the server you're going to have to do some kind of server processing. Best bet would be to have a web service which you can access via JavaScript (AJAX) and you pass your error log info to it.

Doesn't 100% solve the problem cuz if the problem is with the web server hosting the web service you're in trouble, you're other option would be to send the info via a standard page via a query string, One method of doing that is via dynamically generating Image tags (which are then removed) as the browser will try and load the source of an image. It gets around cross-domain JavaScript calls nicely though. Keep in mind that you're in trouble if someone has images turned off ;)

Slace
+4  A: 

I have just implemented server side error logging on javascript errors on a project at work. There is a mixture of legacy code and new code using jQuery.

I use a combination of window.onerror and wrapping the jQuery event handlers and onready function with an error handling function (see: JavaScript Error Tracking: Why window.onerror Is Not Enough).

  • window.onerror: catches all errrors in IE (and most errors in Firefox), but does nothing in Safari and Opera.
  • jQuery event handlers: catches jQuery event errors in all browsers.
  • jQuery ready function: catches initialisation errors in all browsers.

Once I have caught the error, I add some extra properties to it (url, browser, etc) and then post it back to the server using an ajax call.

On the server I have a small page which just takes the posted arguments and outputs them to our normal server logging framework.

I would like to open source the code for this (as a jQuery plugin). If anyone is interested let me know, it would help to convince the bosses!

Karl
I'd be interested in seeing the source
Ryu
I would be interested as well.
orip
Any chance that this code will be made available?
Luke
Unfortunately not :( as I am no longer working for the company where I created that code. But it was only about 100 lines of code, and should be reasonably easy to recreate from the details above.
Karl