background
|
|
Computer

Computer
|
|
Up Image
Navigation
Search this Site
Enter your search terms

Site Breadcrumb - You are here
|
Reference   JavaScript Code Library

JavaScript 1.1 (available in Netscape 3+ and MS IE 4+) adds the ability to modify the browser's default behavior for handling JavaScript errors.

This library module suppresses JavaScript errors so that they can be hidden from view, logged to a server log file for further review, and then displayed in the window.status area if desired.

It requires a CGI on the server side (which can be written in any language you choose) to generate the log. The script passes the error info to the CGI by creating an Image object and setting its source as the call to the CGI. This invokes the CGI script without changing the page or display.

This code contains only one method that can be called directly, and it also contains a number of flags and strings that should be set by the page author upon installation.

Variables:
You should set your own custom values here to suit your particular needs:

  • trapErrors = true;
    Setting this to false will turn off JS error suppression. Useful for debugging during development.
  • logErrors = true;
    Setting this to false will stop errors from being logged. Useful when working offline or without CGI.
  • showErrorAsStatusMessage = false;
    Setting this to true will cause the browser to display the error message as the window.defaultStatus text, resulting in a quieter, but not invisible, error message. Sometimes this can be a more pleasant way to debug JavaScript.
  • logNumErrors = 3;
    Number of JS errors per page to enter into log before calling it quits. Large values aren't recommended ... if you're getting a lot of errors per pageview, then you're probably in an ugly loop.
  • logScriptLoc = "URL to error log CGI";
    This is the location of your CGI script. The script must reside within the htdocs tree or in a server-aliased directory like /cgi-bin, since the error message is sent via HTTP.
  • scriptName = "JS Err Log";
    Optional: This can be any string of text used to identify the script.
  • logComment = "Testing JS Err Log";
    Optional: A short comment may be supplied here.

Usage:
Enabling this code requires three steps:

  1. First, add the code to your HTML page.
  2. Then, create a CGI script to receive the log data (and assign its URL to the logScriptLoc variable). This Perl5 script can serve as an example if you need one. Your script should interpret the data from the JavaScript Image.src call and pass it to ...
  3. ... the last component, the logfile, which resides on your server at a location you have designated in your CGI script.

Tip: If your page makes use of the <body onUnload=""> property, then add a call to restoreWindowToPreviousState() at the end of your onUnload call. Failing to do this won't cause grievous harm, but since this code modifies your page's default properties, it's a good idea to reset them before you exit. If you don't make use of <body onUnload=""> then don't worry about it: This script will call it for you.

Requires: JavaScript 1.1 or later.

License: This code is released under the GNU Library General Public License.

Cut, paste, and enjoy!