如何抑制所有 JavaScript 运行时错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7120290/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:01:09  来源:igfitidea点击:

How to suppress all JavaScript runtime errors?

javascriptruntime-errorsuppress

提问by Skip

How can I suppress all JavaScript runtime error popups, from the programmers side?

如何从程序员方面抑制所有 JavaScript 运行时错误弹出窗口?

回答by Skip

To suppress all JavaScript errors there seems to be a window.onerrorevent.
If it is replaced as early as possible(e.g. right after the head) by a function which returns true - there will be no error popups, which is quite useful after debugging is done.

为了抑制所有 JavaScript 错误,似乎有一个window.onerror事件。
如果它尽可能早地被一个返回 true 的函数替换(例如在 head 之后) - 将不会有错误弹出窗口,这在调试完成后非常有用。

<head>
<script type="text/javascript">
    window.onerror = function(message, url, lineNumber) {  
        // code to execute on an error  
        return true; // prevents browser error messages  
    };
</script> 
...

Error Logging is possible too, as explained here

错误记录也是可能的,如解释here

回答by Leonardo Ciaccio

I made a little script for suppressing an error like "@" does in PHP.

我制作了一个小脚本来抑制 PHP 中“@”这样的错误。

Here is an example.

这是一个例子

var at = function( test ) {
    try {
        if(typeof test === "function") return test();            
        else return test || null;        
    } catch (e) {
        return null;
    }
};