Is it possible to track JS errors that a visitor encounters? Obviously we do our own testing, but from time to time a visitor will be running a certain browser version, or have a particular plugin, at it'll cause a JS error. Likewise sometimes an error in the JS gets to production unnoticed.
It would be extremely useful if we could somehow capture these events, and ajax a script on our server with details of the error so that we could attempt to fix the issue.
UPDATE:
Thanks for the feedback. Typically after posting I managed to find:
Logging Clientside JavaScript Errors on Server
Which had the stuff on window.onerror and also some interesting things about JQuery and a FireFox onerror bug that still hasn't been fixed for years...
Yes, use window.onerror.
For the AJAX logging part, don't rely on any code that might run after the error has occurred. You might want to keep it as simple as possible by making the request with a dynamically-inserted script tag with the source set to be your server script with the data as GET parameters. However, be aware that using GETs will limit you in terms of numbers of characters in the src path (~2000 in IE).
However you do it, it is important to keep it simple as you don't want your error-logging code to be prone to errors in exactly the situations where its most likely to run (the edge cases).
You'll want to disable this in your non-production environments or it could mask problems.
Yes, you can use the window.onerror handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With