Try catch javascript error in whole web application
-
Hi, i need to write a code that would try catch any sudden javascript error in the whole application. i do not want to write the try catch in every single page, is this doable??if so how can i do it??
I'm not sure if there is anything to catch errors globally - but you can simply surround the contents of each of your functions in a try catch statement - or better still a function calling error handling function. Something like: <script> my_function(){ //function code here } my_error_handling(e){ alert(e); } call_my_function(){ try{ my_function(); } catch exception (e){ my_error_handling(e); } } call_my_function(); </script> Then include the script and change it to call any function passed to it.
-
I'm not sure if there is anything to catch errors globally - but you can simply surround the contents of each of your functions in a try catch statement - or better still a function calling error handling function. Something like: <script> my_function(){ //function code here } my_error_handling(e){ alert(e); } call_my_function(){ try{ my_function(); } catch exception (e){ my_error_handling(e); } } call_my_function(); </script> Then include the script and change it to call any function passed to it.
-
yeah i know, but i do not want to catch them pieces by pieces as you said i want to catch them globally so i would prevent any javascript error that suddenly appears in my application.
I found this: http://snipplr.com/view.php?codeview&id=19189[^] function handleError() { alert(An error has occurred!'); return true; } window.onerror = handleError; More[^]