开发者

Hide Javascript error message from browser

开发者 https://www.devze.com 2023-03-22 18:01 出处:网络
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

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>


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号