3

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.

0

2 Answers 2

5

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I love it when there are such easy to implement solutions, saves me hours. thanks a lot, I can now redirect errors to a native error handler. Don't really understand why document is able to perform a callback on crashed pages while no other valid object is able to run... but at least it works...
0

You can use

try{
    adddlert("Welcome guest!");
}catch(err){
    alert(err.message);
}

2 Comments

I'll try the whole webapp (2k lines) inside the try handler.. not sure if runtime errors will be catched this way...
The OP's example is a syntax error though, not a runtime error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.