Chapter 33

Working with JavaScript


CONTENTS


In this chapter you will be introduced to the JavaScript language. You'll examine the relationship between the Java programming language and JavaScript. You will learn how JavaScript and HTML are used to create advanced Web pages. At the end of this chapter, you will have a firm understanding of the constraints, capabilities, and uses of JavaScript.

JavaScript and Java

When choosing a programming language, a primary concern is whether the language can do what you want. When languages overlap in capability, this issue can become cloudy. To solve this dilemma, you need to look at a number of different qualities provided by each approach. If it is still a tie, pick the one that is easier to use. This decision process describes the choice between Java and JavaScript. Both languages supply some of the same capabilities while retaining their own unique functionality.

The first obvious difference between the two languages is that JavaScript is a scripting language, whereas Java is fully compiled. Both Java and JavaScript retain the majority of the same C-type syntax, but JavaScript is a loosely typed language and Java is strongly typed. JavaScript is referred to as loosely typed because it does not require that the type of a variable be identified when the variable is declared and because it automatically converts values of one type to another. Java is referred to as a strongly typed language because it requires that all variables be declared to be a unique type and because it does not automatically convert values from one type to another.

Both Java and JavaScript support the same primitive data types: numeric, string, and boolean. They also support functions in a slightly different way. Due to the loosely typed syntax of JavaScript, function definitions are also loosely typed. This means that the type of the return value of a function does not have to be specified.

Java and JavaScript provide similar capabilities. Each does, however, offer some unique capabilities. For JavaScript the majority of these capabilities pertain to its ability to exist within an HTML document. Java's strengths stem from the fact that it is a compiled language and, as such, is not confined to the limitations of a scripting environment. Table 33.1 shows a comparison of the two languages.

Table 33.1. Java versus JavaScript.

JavaJavaScript
CompiledInterpreted
Accessed from HTML Embedded within HTML
Strongly typedLoosely typed
Uses static binding Uses dynamic binding
SecureSecure

How JavaScript Works

Unlike Java, whose code is compiled and stored in a .class file, the JavaScript code is embedded inside an HTML document. A Java applet is executed within a separate applet window of the browser's display. JavaScript, on the other hand, becomes an active element of the HTML document, and in doing so, has the capability to modify elements of the displayed Web page.

When the browser sees the JavaScript tag inside the HTML, it begins compiling the information needed to interpret the script. The script does not begin execution until the entire page has been laid out, at which time the script begins executing. It is true that the script has the capability to modify the appearance of the current page; however, it does need to abide by the update policy of the browser. After the browser has laid out the page, the script has to reload the page before any changes can be seen. There is a way around this: The script can update a region inside a frame without updating the entire page.

JavaScript is a powerful tool for Web development, but it shouldn't be viewed as a direct alternative to Java. Rather, Java and JavaScript can work in tandem to provide a wide range of application possibilities.

The Relationship Between HTML and Scripts

With Java applets, HTML serves as a command line of sorts to execute the applet. From the HTML command line, parameters can be sent into the applet. However, the communication with HTML is strictly one-way. The applet does not have the capability to modify the content of the HTML document. Herein lies the distinct difference between JavaScript and Java.

JavaScript is a script language that resides and is executed from within the HTML code. You can think of JavaScript in the same way as any other HTML language element: A script is interpreted and then executed from within the browser. In this manner, the script is handled in the same way as a frame. Executing the script from within the browser allows the script to have access to other elements of the HTML page-in a sense, allowing the script to dynamically change the content of the page.

Embedding JavaScript

To add a script to an HTML document, you use the script tag. It is a surrounding tag and is written as <SCRIPT> and </SCRIPT>. The LANGUAGE attribute is set to the value JavaScript to indicate that the script is a JavaScript script. All JavaScript code must be contained between the opening and closing script tags, as shown in the following example:

<SCRIPT LANGUAGE="JavaScript">
JavaScript statements...
</SCRIPT>

Note
Chapter 16, "Web Programming with the java.applet Package," provides a brief introduction to HTML tags.

Browsers that do not support JavaScript should ignore the statements contained within the script tags. The LANGUAGE attribute can be omitted because JavaScript is currently supported only by Netscape browsers and is the only scripting language supported by these browsers. However, it is good practice to include the LANGUAGE attribute in case other scripting languages become widely supported.

JavaScript objects can be placed wherever you want on a Web page; however, it is a good idea to declare all functions in the head portion of the document. This assures that the functions are declared before the page is displayed, eliminating the possibility that a user might execute a function before it has been declared.

Combining Scripts with Applets

JavaScript has the capability to dynamically alter a Web page by generating new HTML elements to be displayed. Whether the HTML is created in response to an event or simply generated during the course of the script program, the HTML generated can be used to update a Web document's display. Keep in mind that applets are executed from an HTML tag reference. This means that HTML generated by JavaScript could very well execute a Java applet.

By launching an applet in response to an event, your HTML has the capability to take on an entirely new level of functionality. If the concept of launching an applet from a script seems a little confusing, see the examples in Chapter 34, "Sample Scripts."

Summary

In this chapter you have been introduced to the JavaScript language. You have seen how JavaScript is embedded into HTML documents. You have learned the differences between JavaScript and Java, as well as the strengths and weaknesses of both. You have also learned how applets and JavaScript can function together to create attractive Web pages. In Chapter 34, you will see examples of JavaScript and will be provided with the in-depth information you need to create your own scripts with JavaScript.