从浏览器中隐藏 javascript 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6805798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hide javascript error from browser
提问by tugberk
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 freindly 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
我不知道存在这样的事情,我尝试使用 try-catch 块。发生错误后,我意识到浏览器仍然显示错误。如何从浏览器中隐藏错误
UPDATE
更新
I tried following code and put that inside the head section of my all pages but noting happened;
我尝试了以下代码并将其放在我所有页面的 head 部分中,但注意到发生了;
<script type="text/javascript">
(function () {
window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
alert(errorMsg);
// Just let default handler run.
return true;
}
})();
</script>
回答by beardhatcode
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
更多:https: //developer.mozilla.org/en/JavaScript/Reference/Statements/try...catch