jquery - Hide Javascript error message from browser - Stack Overflow

I found this code sample on one of the questions;window.onerror = function (msg, url, linenumber) {

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>

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>
Share Improve this question edited Mar 21, 2021 at 22:45 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 24, 2011 at 8:41 tugberktugberk 58.5k70 gold badges251 silver badges342 bronze badges 6
  • Re: your updated question - nothing will happen until there is an error. – Adam Hopkinson Commented Jul 24, 2011 at 9:04
  • @adam :) of course I raised an error but still nothing happened – tugberk 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. – tugberk Commented Jul 24, 2011 at 9:07
  • 2 They will still appear in the js console - that's what the console is there for. – Adam Hopkinson Commented Jul 24, 2011 at 9:14
  • @adam no way to hide I guess or we need to dive deeply. – tugberk Commented Jul 24, 2011 at 10:25
 |  Show 1 more ment

3 Answers 3

Reset to default 5

use: try...catch

function message()
{
    try
    {
        adddlert("Wele 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/en/JavaScript/Reference/Statements/try...catch

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

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./edit/web-platform-mfattr?file=index.html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743711031a4494183.html

相关推荐

  • jquery - Hide Javascript error message from browser - Stack Overflow

    I found this code sample on one of the questions;window.onerror = function (msg, url, linenumber) {

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信