12

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

Skip
  • 6,240
  • 11
  • 67
  • 117
  • 3
    You can try to wrap your whole code into a `try...catch` block. But it is better to fix errors ;) – Felix Kling Aug 19 '11 at 10:33
  • sometimes there is not possibility to use `try...catch`, for example if some external library throws errors. Using window.onerror event is the solution in this case, see my answer – Skip Aug 21 '11 at 12:05

2 Answers2

23

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

Skip
  • 6,240
  • 11
  • 67
  • 117
  • 2
    _The HTMLDocumentEvents4::onerror event fires for run-time errors, but not for compilation errors. In addition, error dialog boxes raised by script debuggers are **not** suppressed by returning true._ from [MSDN](http://msdn.microsoft.com/en-us/library/ff976266(v=vs.85).aspx) – Bakudan Oct 14 '13 at 14:16
  • This method doesn't seem to work for Angular 2.x+ apps - likely due to the Angular handling the errors itself. – Levi Fuller Jul 19 '17 at 01:45
3

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;
    }
};
Angel Politis
  • 10,955
  • 14
  • 48
  • 66
Leonardo Ciaccio
  • 2,846
  • 1
  • 15
  • 17