Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert Javascript Errors when javascript fails?

Tags:

javascript

When I have a fatal error in javascript making the app not usable, like this one:

SyntaxError: Unexpected token '}'

Is it possible to catch it and display it somehow? like replacing the body or alerting?

So in other words, a way to run javascript code if there is a javascript code error...

I can't open the web inspector on runtime when I'm inside a crashed page, this is because I'm using webkit outside of a browser who would normally be able to open and display the webinspector on a crashed page.

like image 812
Vincent Duprez Avatar asked May 26 '26 21:05

Vincent Duprez


1 Answers

Define window.onerror callback in the very first <script> loaded on the page, like this:

<script>
window.onerror = function(msg, url, line) {
    alert(msg + ' appeared on the line #' + line);
}
</script>
like image 109
raina77ow Avatar answered May 28 '26 09:05

raina77ow