Handling JScript Run-Time 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.

In Microsoft® JScript®, you can use the window object's onerror event to trap an error and respond accordingly. The onerror event is triggered when an error occurs anywhere on an HTML page. The onerror event passes three arguments to the function specified by the window.onerror = functionname; line. The arguments are the error message itself, the URL representing the source of the error, and a number representing the line where the error occurred. For example:

   function BadFunction(){
      This.badcode.willnot.work = 1000

   function ForceError(msg, url, lno) {
      alert("Error Occurred! Handled by Generic Error Handler" + "\n" +
         "Error: " + msg + "\n" + "URL: " + url + "\n" +   
         "Line Number: " + lno);

      return true;

   window.onerror = ForceError;

Note   Returning True in the error-handler procedure forces the default message box not to appear. For more information about scripting HTML and ASP pages by using VBScript or JScript, see the Microsoft Scripting Technologies Web.

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