gpt4 book ai didi

javascript - 从浏览器中隐藏 Javascript 错误消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:27:43 25 4
gpt4 key购买 nike

我在其中一个问题上找到了这个代码示例;

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;
}

“从浏览器中隐藏错误” 是什么意思,我该怎么做?

我不知道有这样的东西存在,我用 try-catch block 试了一下。发生错误后,我意识到浏览器仍然显示错误。如何隐藏浏览器错误?

更新

我尝试了以下代码并将其放入我所有页面的头部部分,但注意到发生了;

<script type="text/javascript">

(function () {

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

alert(errorMsg);

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

})();

</script>

最佳答案

使用: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);
}
}

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

关于javascript - 从浏览器中隐藏 Javascript 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6805798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com