How to suppress all JavaScript runtime errors?

大兔子大兔子 提交于 2019-12-17 19:35:18

问题


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


回答1:


To suppress all JavaScript errors there seems to be a window.onerror event.
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.

<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




回答2:


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

Here is an example.

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


来源:https://stackoverflow.com/questions/7120290/how-to-suppress-all-javascript-runtime-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!