Handling Script Errors

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

You have limited run-time error-handling options with script behind an HTML page, and the conditions that generate errors can differ depending on the scripting language you use. For example, in Microsoft® Visual Basic® Scripting Edition (VBScript), dividing by zero results in a run-time error. In Microsoft® JScript®, it does not.

In VBScript, you are limited to basic in-line error handling using the On Error Resume Next statement. You can suppress default error messages, but you cannot specify your own error-handling code to run when an error occurs. After you have trapped and handled an error, you use the Clear method of the Err object to clear out the error information.

In JScript, you can handle errors by using the window object's onerror event handler. The onerror event passes three arguments to the specified error-handling function: the error message itself, the URL representing the source of the error, and a number representing the line where the error occurred.

There are also special circumstances you should keep in mind when writing script and script-error handlers.

Script in an HTML page is not interpreted until the script is executed, which occurs at the earliest when an HTML page is loaded. This means that the language interpreter will not catch any syntax errors until the page is actually loaded.

Subtle logic errors can result from the required use of Variant variables with different data subtypes. For example, the statement "10" = 10 evaluates to True.

Script in the <HEAD> portion of the page is interpreted before script in the <BODY> portion of the page. A run-time error can occur if script in the <HEAD> portion of a page references elements within the <BODY> portion of a page and the script executes before the page is completely loaded.

Web pages consist of a number of different components that might be downloaded at different times. Because of this, you cannot always be sure that an element of your page referenced in script is going to be available when the script executes. For example, if you use framesets, objects, images, linked style sheets, applets, or plug-ins, you must verify that the component is available before performing operations that depend on the component.

See Also

Writing Error-Free Code | Design-Time Tools | Run-Time Tools | Script Debugging Tools | Basic Error Handling | Getting Information About an Error | Error Handling in Class Modules | Handling VBScript Run-Time Errors | Handling JScript Run-Time Errors