Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if the browser has encountered a JavaScript error ... in JavaScript?

For automated testing reasons I want to detect if the browser has encountered JavaScript errors for a page. The type of things that would cause the red numbers in the bottom right in Firebug or yellow warning icon in the Internet Explorer status bar. These JS errors could come from any one of a large numbers of scripts.

How in JavaScript can I detect these browser errors? Cross browser solutions prefered, but hey, I'll take anything!

As a bonus it would also be great to know the script that caused the error.

like image 452
RichH Avatar asked Apr 02 '10 20:04

RichH


People also ask

How do I check for JavaScript errors?

Right-click anywhere in the webpage and then select Inspect. Or, press F12 . DevTools opens next to the webpage. In the top right of DevTools, the Open Console to view errors button displays an error about the webpage.

How do I see JavaScript errors in Chrome?

In Chrome, navigate to Tools > Advanced > Error Console. The error console will open. Select JavaScript and Errors from the two drop downs. To find the error location, expand one of the errors.

How do I see JavaScript errors in Safari?

To show the menu bar, click Settings > Show Menu Bar. The menu bar can be removed at any point by clicking Settings > Hide Menu Bar. To use the console, open Safari and in the menu bar choose Develop > Show Error Console.


1 Answers

window.onerror handler is the answer. Read this - http://www.javascriptkit.com/javatutors/error.shtml

To get more information about the script that caused the error, read this - http://www.javascriptkit.com/javatutors/error3.shtml

like image 165
Sripathi Krishnan Avatar answered Oct 12 '22 14:10

Sripathi Krishnan