3

I found this code sample on one of the questions;

window.onerror = function (msg, url, linenumber) {  

 //make ajax call with all error details and log error directly into the elmah database

 //show friendly error msg here to user

 //hide error from browser
 return true; 
}

What does 'hide error from browser' means and how can I do that?

I didn't know such a thing exists and I give it a try with try-catch block. After an error occurred, I realized that browser still shows the error. How can I hide error from browser?

UPDATE

I tried following code and put that inside the head section of my all pages but noting happened;

<script type="text/javascript">

(function () {

    window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {

        alert(errorMsg);

        // Just let default handler run.
        return true;
    }

})();

</script>
6
  • Re: your updated question - nothing will happen until there is an error. Commented Jul 24, 2011 at 9:04
  • @adam :) of course I raised an error but still nothing happened Commented Jul 24, 2011 at 9:05
  • @seoul on Chrome, when I open up the js console, errors are still there when I use try-catch block. I don't want that. Commented Jul 24, 2011 at 9:07
  • 2
    They will still appear in the js console - that's what the console is there for. Commented Jul 24, 2011 at 9:14
  • @adam no way to hide I guess or we need to dive deeply. Commented Jul 24, 2011 at 10:25

3 Answers 3

5

use: try...catch

function message()
{
    try
    {
        adddlert("Welcome guest!");
    }
    catch (err)
    {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.description + "\n\n";
        txt += "Click OK to continue.\n\n";
        alert(txt);
    }
}

more: https://developer.mozilla.org/en/JavaScript/Reference/Statements/try...catch

Sign up to request clarification or add additional context in comments.

Comments

2

When the function returns true, this prevents the firing of the default event handler.

It's just the way the browser behaves if this function returns true.

See more

Comments

1

Adding window.onerror function to the head section and returning true would stop logging the errors events to the browser console.

As @Decko mentioned returning true prevents the firing of the default event handler.

Stackblitz - https://stackblitz.com/edit/web-platform-mfattr?file=index.html

Comments

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.