header.gif (6771 bytes)

Lesson 23- Suppressing JavaScript errors in a document

This is it, a JavaScripter's dream come true. A special property actually exists in JavaScript that allows us to suppress all errors in a page, getting rid once-in-for-all those annoying error messages. In this section, we will look at:

Red_CurlyC035.gif (285 bytes) Understanding the onerror event

The key to suppressing errors in JavaScript lies in the onerror event handler. Just to clarify, when we say suppress errors, we mean suppressing those error messages associated with bad scripts. Just in case you've drifted into la la land, an event handler is a piece of JavaScript code that handles and executes codes as the specified event occurs, such as the clicking of the mouse, loading of the page etc. The onerror event is supported by NS 3.x, 4.x, and IE browsers. In this section, we will first discuss in general what the onerror event is, and in the next section, we'll use it to actually suppress errors.

The syntax required to use the onerror event is a little different:

<script>
window.onerror=function name here
</script>

The onerror event is attached directly to the window object, and is declared inside the script tags, unlike other event handlers. The code to be executed as a result of an error has to be first declared inside a function, and than the function itself attached to the event (only the function name, without the usual brackets), as shown above. One word that may come to mind right now is: "Why?" Why all this weird syntax, just to use an even handler? Well, the onerror event is an event designed specifically for the window object, and since there doesn't exist a window "tag"  in the document, an alternative is needed to place the event handler in.

Its perfectly natural to feel a little baffled at this stage; lets see a simple example using the onerror event, and it will all make sense:

<html>
<head>
<script>
function errorreactor(){
alert("An error has occurred!")
}
window.onerror=errorreactor
</script>

</head>
<body>
<script>
document.write("This is a bad script!"
</script>

</body>
</html>

The "document.write" script above is illegal (no closing bracket), so an error will be generated. Using the onerror event, we detected when that happened, and told JavaScript to alert a custom message when the error occurred.

Red_CurlyC035.gif (285 bytes) 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.  The following modifies the above example so the default error message is suppressed:

<head>
<script>
function errorsuppressor(){
return true
}
window.onerror=errorsuppressor

</script>

<script>
//invalid code below, although no errors will be generated
document.write("This is a bad script!"
</script>

</head>

Using the onerror event, along with function "errorsuppressor", any scripting error in a page will be suppressed. Notice the placement of the onerror code; it is declared prior to any other codes, and wrapped around in its own <script> tags. This is to ensure that it intercepts all scripting errors in the page.

Be careful when using the onerror event with scripts, 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!

Lesson 24