Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Debugging JavaScript programs

A quick reference guide that helps you find errors, <br>even without the aid of a debugging utility

  • Print
  • Feedback

Page 3 of 7

Type single-line command entry in the Location box
A little-known JavaScript testing shortcut is to type a JavaScript command line in Navigator's Location box. Preface the command line with the JavaScript: protocol, like that shown below, and press the Enter key to see the result:


javascript:alert ("this is a test")


Note that not all command lines provide meaningful results. For example, typing


javascript:document.write ("this is a test")


and pressing Enter results in true printed in the document window.

If you need to test a number of command lines, you can reduce the keystrokes by typing javascript: (remember the colon) in the Location box. Navigator displays a "JavaScript typein" frame at the bottom of the window. Type the command line you want to try, and press Enter.

Type multiple-line commands using a helper script
The "JavaScript typein" frame window described above is defined within Navigator. You can create your own version of it to allow for simplistic multiple-line command entry (this script is very basic and can be improved, but it serves to get you going). The following displays a multiline JavaScript typein utility. The multiline command entry script is a combination frameset and JavaScript-generated frame document. To use, type the script segment you want to test in the text-area box, and choose Run.

Note: When using this script be sure to append semi-colons (;) at the end of each line. This helps JavaScript properly parse the command lines you enter.


<SCRIPT>
function jsinput () {
parent.MochaInput.document.write ("<b>JavaScript input</b>")
parent.MochaInput.document.write ("<form action=JavaScript: target=MochaOutput>")
parent.MochaInput.document.write ("<textarea name='isindex' rows=5 cols=50>")
parent.MochaInput.document.write ("</textarea><BR>")
parent.MochaInput.document.write ("<input type='submit' value='Run'>")
parent.MochaInput.document.write ("</form>")
}
</SCRIPT>
<FRAMESET ROWS="50%%,50%%"> <FRAME NAME="MochaOutput" SRC="about:blank"> <FRAME NAME="MochaInput" SRC="javascript:parent.jsinput()"> </FRAMESET>


Make paper copies
JavaScript programs are composed of text embedded in HTML documents. Therefore, you can use any text editor program that has a print capability to make paper copies of your scripts. The paper copies may help you find errors that are otherwise hidden in screenfuls of code displayed on your monitor.

You may find it helpful to print your JavaScript programs in a monospaced font, such as Courier, to help spot such mistakes as a string that is empty but should contain a space. The monospace font is better suited to locating these kinds of errors, but it does takes up more space on the line. Use a small 9- or 10-point font to compensate.

Use a simple object inspector
The for/in statement can be used to create a simple object inspector. The following short script is all you need to inspect a JavaScript object. For example, to get a listing of the current properties of the window object, load the script and type window in the prompt box. Choose OK. JavaScript displays an alert box listing each property of the window object.

  • Print
  • Feedback
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?

Resources
  • Yahoo's JavaScript pages
    http://dir.yahoo.com/Computers_and_Internet/Programming_and_Development/Languages/JavaScript/
  • Be sure to check the Release Notes for Netscape Navigator by choosing Help, Release Notes in the Navigator menu. The Release Notes contain a listing of known bugs. (The list is incomplete, however.) The known-bugs listing can help you avoid many pitfalls in JavaScript programming.