0

Is it possible to track the number of errors and warning thrown by a browser on page load using javascript?

For example CSS Assets missing or images missing or js script errors? I don't need a detailed log but maybe a count of each type of error.

Like a count of CSS, Assets missing and JS Errors?

This needs to be tracked using JS and not in the console. The script needs to collect all these errors from the console and send them back to my server.

sarovarc
  • 399
  • 1
  • 5
  • 12
  • Haven't you used a browser console before ? – Rohit Bandooni May 29 '15 at 12:00
  • Yeah... As Rohit said check in developer console and if you need that data at backend check your logs in server. – Vivek May 29 '15 at 12:01
  • Sorry. Should have clarified, this data needs to be tracked in a database on every page load. So this is not for development; but more in terms of checking on the health and performance of every page. – sarovarc May 29 '15 at 12:02
  • Check at server logs, look for 404s as i said above. – Vivek May 29 '15 at 12:03
  • The tracking script needs to be triggered at client side. Is there a way a script can track how many and what type of errors are being caused on a page load? – sarovarc May 29 '15 at 12:05
  • possible duplicate of [JavaScript: How do I print a message to the error console?](http://stackoverflow.com/questions/164397/javascript-how-do-i-print-a-message-to-the-error-console) – ozil May 29 '15 at 12:14
  • Try using something like rollbar.com service. It has JS integration – Stanislav Mekhonoshin May 29 '15 at 12:14

1 Answers1

3

Yea, absolutely. There are a number of ways to do this.

For JavaScript, you can capture errors by listening to window.onerror. This will give you the error information and you can AJAX the data back to your infrastructure.

If you don't want to support the infrastructure log them yourself, you can use a JavaScript Error Logging service like TrackJS to do this automatically.

For Asset load failures, you can attach onerror listeners to the css tags, although this won't work in all browsers. You could send the load failures back to the same logging infrastructure.

You should also check out the new proposed standard on Network Error Logging, which might be relevant for what you're trying to do.

Todd H. Gardner
  • 630
  • 4
  • 18