0

Is there any way to catch Javascript errors globally across the web application by writing some code in the master page?

AnthonyWJones
  • 183,100
  • 34
  • 230
  • 304
  • 1
    Dup: http://stackoverflow.com/questions/951791/javascript-global-error-handling and http://stackoverflow.com/questions/546990/automatic-feedback-on-javascript-error and http://stackoverflow.com/questions/205688/javascript-exception-handling and http://stackoverflow.com/questions/119432/logging-javascript-errors-on-server and – Crescent Fresh Sep 04 '09 at 13:30
  • Possible duplicate of [JavaScript global error handling](https://stackoverflow.com/questions/951791/javascript-global-error-handling) – Michael Freidgeim May 02 '19 at 21:42

2 Answers2

8

You can use the onerror event:

var oldOnError = window.onerror;
window.onerror = function()
{
    alert('Something bad happened');

    //Optionally...
    oldOnError();
}
Greg
  • 307,243
  • 53
  • 363
  • 328
  • 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 Feb 02 '10 at 19:23
  • Do you possibly know why this simple demo - http://jsfiddle.net/YM9U7/ - works in IE9 beta and Firefox 3.6, but doesn't work in Chrome, Safari and Opera? (By "works" I mean, the "Error!" alert is displayed.) – Šime Vidas Feb 04 '11 at 20:08
0

See JavaScript Exception Handling Techniques

Dan Dascalescu
  • 127,343
  • 44
  • 300
  • 387
Ramesh Soni
  • 15,703
  • 26
  • 91
  • 113