Book Section
-----
TY JavaScript 3rd Ed.
Teach Yourself JS 1.5
Teach Yourself DHTML
Teach Yourself JS 1.3
LLWW: JavaScript

General Section
-----
Discussion Forum
Articles / Tips
JavaScript Links
About the Author
Privacy Policy
Contact Me



Other Sites
-----
Website Workshop
JavaScript Weblog

Article: Testing JavaScript Interactively

Testing JavaScript Interactively

September 20, 1996

Sometimes it's handy to test a bit of JavaScript without writing a program. An undocumented feature of Netscape is a command line that allows you to do just that. Just type mocha: or javascript: in the URL box and you'll be presented with a command-line interface. You can type any JavaScript command, define variables, and so on. The result of each command or expression you type is shown in the upper frame. Follow this link to try it out (Netscape only).

(This feature is described in more detail on Pages 255-257 of Web Workshop: JavaScript)

The JavaScript Test Console

Unfortunately, MSIE doesn't support a similar command line, so this technique is useless for comparing results with different browsers. I've created a solution called the JavaScript Test Console. This is very similar to Netscape's built-in command line, but works with any JavaScript-capable browser. Use the URL below to access the test console:

Features

This is a freeware utility for testing JavaScript statements and expressions in Netscape 2.0, Netscape 3.0, or MSIE 3.0. It can be used online at the URL above or copied to your local machine.

The JavaScript Test Console works with both Netscape (2.0 or 3.0) and MSIE 3.0; it also provides slightly neater output including the command that produced the output. Basically, it's a quick way to test a few statements without creating an HTML file.

You may find this useful for:

  • Testing statements such as alert("hello") in real time
  • Checking if properties (such as navigator.userAgent) exist or displaying their values
  • observing differences between MSIE and Netscape

Using the JavaScript Test Console

TO USE IT: Type something in the blank and press the "Test Expression" button. If you type a command, it will be executed; if you type an expression, it will be evaluated. The command and result for the last 8 expressions are displayed in the upper frame.

TO STEAL IT: You'll need to save copies of these three files, and keep them in the same directory:

  1. jsprompt.html (frameset)
  2. prompt.html (bottom frame)
  3. blank.html (top frame)
(the top frame is blank, but MSIE doesn't support about:blank)

The JavaScript Test Console is copyrighted, but freely available. Feel free to use it, put it online on your own server, or modify it. Please contact me via email if you have made any useful modifications to it; I may want to include them myself. The only thing you can't do is claim you wrote it.

How it Works

To see the JavaScript source for the Test Console, view the source of the prompt.html file. This simply uses the eval function to evaluate or execute the commands or expressions you type in the form. The rest of the code keeps track of the most recent 8 commands, and displays them with their results (without executing them again) in the upper frame.
(c) 1997-2002 Starling Technologies and Michael Moncur. Portions (c) Sams Publishing.