|
CodingForums
Having trouble with scripting? Visit our help forum to get the answers you need.
This is a 
|
|
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 create a function that returns
a value of "true", and attach this function to the onerror event. This
function should appear at the very top, proceeding any other
JavaScripts on the page. The
following modifies the above example so the default error message is
suppressed:
<head>
<script type="text/javascript">
function errorsuppressor(){
return true
}
window.onerror=errorsuppressor
</script>
<script>
//invalid code below, although no errors will be generated
document.write('hi there'
</script>
</head>
Using the onerror event, along with a function that returns a value of
true, any
scripting error in a page will be suppressed.
Be careful when using the onerror event this way, since it only
suppresses errors, but doesn't’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!
|