JAVA FOR EVERYONE:
Javascripting Javascript

by Mona M. Everett, Ph.D. (everett@txdirect.net)

Copyright © 1996 Mona M. Everett. All rights reserved.


In a perfect world, one programming language would do for all of the flavors of computer platforms and operating systems. Wouldn't it be nice if an application could run unmodified on four versions of Microsoft Windows, two of the Macintosh, OS2, and a dozen incarnations of UNIX? Dream on—or maybe not! Think for a moment about Web browsers in general and Netscape Navigator in particular. What do you have? It runs on four versions of Microsoft Windows, two of the Macintosh, OS2, and a dozen incarnations of UNIX. Because of the expanding importance of the World Wide Web, it was only a matter of time before someone decided to give a Web browser a programming interface. The first attempt was Java, developed by Sun Microsystems, and available for several platforms. Although Sun originally developed their own browser, Hot Java, to run the language, a collaboration with Netscape facilitated development of a Java- enabled Navigator. Java "applets" must be compiled by the developer. They can then be downloaded to the machine on which a browser runs and "displayed/played," much like a GIF, by the virtual machine (translation: interface between machine specific code and Java byte code) built into the browser. Java, unlike C++ is truly object oriented. It has lost most of the deficits of C/C++ although, unfortunately, has kept the terse and often cryptic syntax.

Javascript might be thought of as a "little brother/sister" of Java. It has the same syntax but only a subset of Java's features. Unlike Java, Javascript is interpreted, rather than compiled. The code is part of the HTML which resides in and determines the form of the Web page. In fact, you can have a page which is nothing but Javascript and even have Javascript write the page. Javascript, like Java, is object oriented, and a version of it exists for all of Netscape Navigator's platforms.

The Microsoft Windows world hosts three major "friendly" object-oriented and GUI-based languages and a plethora of others less often seen. The major players are Visual Basic, Powerbuilder, and Delphi. All of these, though, were much preceded by Apple's Hypercard which, to my way of thinking, is a considerably more powerful language than any of the Windows latecomers as long as one is not interested in (1) inherent color and (2) database access. Hypercard was originally developed for the original old black-and-white Mac long before color monitors and ODBC became as common as weeds. Hypertalk (the scripting language of Hypercard), though, has a feature which is not inherent to any of the aforementioned Window's languages. Hypertalk can write Hypertalk. Not only can it write Hypertalk, it can even alter its own scripts. While conservative programming aficionados might gasp at the concept (along with goto) smart-aleck assembly language programmers know that that is one of the fastest ways to write code, and goto, in the guise of jmp, is often the only game in town. But now that I have gotten in my dig at "proper" programming techniques, lets talk about Javascript, which, by the way, sadly, has no hint of a goto statement.

Javascript is a language in its infancy. It has no hint of the nice drag and drop, click and code, interfaces offered by Visual Basic and Delphi, although those niceties are on the way. Check out Adobe's PageMill and Netscape's Navigator Gold betas, to mention two. It is also, at present, a very sparse language. You have to work hard to produce sophisticated effects and in the process, find bugs of which neither Netscape nor anyone else ever dreamed. Of course, some of these latter are claimed as "features." While C programmers will be right at home with the language, if you have never written code before, or if BASIC or Hypercard are more your style, Javascript, which embodies C's terseness, will look like someone's mathematical nightmare. If you have never seen C code, suffice it to say that C/C++ are often described as write-only languages. Reading them, even if you, yourself, wrote it last week, can be quite a chore. Further, since Javascript is interpreted, it is also somewhat slow. So with all of these negative features, what is so great about Javascript?

The purpose of this column will be to teach you Javascript. However, there will soon be a number of books out on Javascript, one of which I have contributed to; there are many good Web sites; and Netscape's documentation, while it never keeps pace with the changes in the language, is well written and a good place to start. You really do NOT need another place to read about how to construct simple Javascript; therefore, this column will try to teach you some of those "powerful aspects" of the language. Besides, I never start at the beginning...but just in case you DO, I will try to comment code well enough for you to follow it if you are just starting out. I will also make the assumption that readers are already familiar with HTML. With that, let's jump right in and write some Javascript. Script Tags Javascript is usually but not always written between script tags, i.e.

Although script tags can appear anywhere, they most often appear in the document header. They may also be at the end of the document or anywhere within the text. However, there are some places within the HTML text where scripts might not be interpreted or might be misinterpreted so use script tags within the document body with caution. Here is a simple layout for an HTML Document with header and footer script tags included. Type or cut and paste this template into your favorite text or HTML editor:

The template given above has examples of both HTML comments and Javascript comments. HTML text comments are placed like this:

Javascript comments are similar to C comments; there are two ways to make them. You can either use

which will do for either a single line or multiple lines of text, OR

"//" will only comment out the line of text on which it appears.

Debugging

Not a chance!

You can only debug with old-fashioned write line debugging, but there is no convenient debug window to write lines to. You can, however use an alert box to visualize your debugging output. This is also a way to provide feedback to your user. So here is your first Javascript script (we won't count the comments):

Put this script in a document header script of the skeleton given above and reload the document. As soon as the document reloads, the alert box will pop up. Note that it does so before anything else is written, since it is in the header. After you dismiss the alert box by clicking the OK button, the script finishes and the body of the document is written.

Variables

Three variables (astr, bstr, cstr) were used to hold the text placed into the alert box. Think of variables as containers which can hold text, numbers, or booleans (true/false). In many languages, variables are strongly typed; i.e., you have to declare (which makes) the variable (container) and say what type it is before you can use it. An analogy might be that you must buy a pie plate, which is designed specifically for pies, before you can bake the pie. Javascript, like BASIC and Hypercard are untyped languages. That is, one container fits all. Like a good plastic bag, you can put anything into it; the container loosely conforms to fit its contents. Variables which hold text are called strings; variables which hold numbers are called numerics; and variables which contain true/false values are called booleans. Although not foolproof, the Javascript interpreter will try to figure out what you are doing and convert as necessary. Sometimes you need to help it along, though. Try this between script tags in the template and reload the page.

First, an alert box should pop up and say that the number is 67. Since the first item was a string, the interpreter assumed concatenation and did not actually add 6 + 7. Once you dismiss the alert box, another should pop up and give you the number 25. The interpreter now treats the variables as numerics, since they could represent numbers and no strings are involved. Then a third box pops up and presents you with the correct addition, since you did it separately. If, perchance, you would like two numbers to be concatenated instead of added, concatenate them to the empty string, ''; e.g.,

Well, now that you know about alert boxes and variables, let's find a better use for a header script then showing an alert box. Substitute the following for a header script in the simple document template and reload the document.

You should see this at the top of your newly rewritten page, followed by the body text.

Now that's useful!

Paste this script into the header of a template document and Javascript will always write the header for you. Just remember to define the title between title tags in the header PREVIOUS TO THE SCRIPT. Before we close, let's go over the new items in the script. You should already be familiar with TABLE tags, FONT tags, BReak tags and HR (horizontal rule) tags.

Operators

Javascript has the usual complement of mathematical operators such as +, - , * , and / . As you have seen, + can also be used to concatenate strings. A new compound operator, += was introduced in this example. Such operators are what cause C to be called "terse." Instead of saying

we say

There are many other operators, which you should review.

Document

The document is the blank page upon which your browser writes. It has a number of components (things that it owns, like a title and a location ) and methods (things that it can do to itself, like open, close, and write). The most used is document.write(something). The "something" can be a variable which you have composed

or a string literal

or a combination

Nested Quotation Marks

In the script, we used this line

For quotation marks, you can use either ' or ". If you need to quote something within a quote, you must switch to the other form; i.e., you could have also used

If you need a third level of quotes, you may use \" (escaped quotation mark). Another escape sequence commonly used is \n, which begins a new line.

Well, that's all for today. Already, you have learned how to significantly enhance your Web pages with Javascript. Next time, we will learn how to put the header write code into a hidden frame as a Javascript function, and we will enhance the header with more features. Most importantly, we will learn how to open a new window under Javascript control.