Categories:

Using the onerror event to suppress JavaScript errors

Now that we know how to detect when an error has occurred (by using the onerror event), we can suppress them. Simply have your function attached to the onerror event return true at the very end. The following modifies the example on the last page so the default error message is suppressed:

<head>
<script type="text/javascript">

window.onerror=function(){
 alert('An error has occurred!')
 return true
}
</script>

<script type="text/javascript">
document.write('hi there'
</script>
</head>

Defining the onerror event with a function that returns a value of true at the very top of your page suppresses all scripting errors on the page .

Be careful when using the onerror event this way, since it only suppresses errors, but doesn't fix them. Even humans have trouble with the later! Whenever testing codes in your browser, make sure to first turn off the error suppressor , or you may even be fooled by your foul scripts!