Chapter 18

JavaScript and the Internet

by Bill Anderson


CONTENTS

Until the proposal of the Multipurpose Internet Mail Extensions (MIME) protocol in 1993, the Internet was a text-oriented environment. The only way to view non-text files was by downloading files via File Transfer Protocol (FTP). Even the Gopher and the first HTML 1.0 protocols require FTP to transfer image files. Although the MIME RFC is an extension to the mail format, the advent of MIME opened the door for a way to introduce non-text documents into Gopher and HTML. The NCSA Web browser provided the first graphical interface for the MIME-enhanced HTML.

Whether it was graphics, sound, movies, word processing documents, or postscript documents, the MIME format gave the information provider a multitude of options for presenting information to the consumer. Using the MIME types, people entered millions of documents to create a virtual worldwide library without the restrictions of traditional information media, but it was not enough. Traditional media lacked a way to interact with the information consumer. Although a lot of files included the mailto: scheme in their URL as a method for user feedback, the document was still static. The section on CGI scripts shows the first major step toward creating interactive HTML. With CGI, the server handles all decisions. The client side (the browser) acts only to input data to the CGI script and display any resulting output. The HTML document itself contains no programs or decision logic. The introduction of Java and JavaScript changed the fundamental nature of the HTML document itself.

What Is JavaScript?

Netscape introduced JavaScript as LiveScript in the Netscape Navigator 2.0 beta. When it released version 2.0, the name changed to JavaScript. At the time of its release, many companies hailed the new scripting language as a major step toward enhancing the capabilities of HTML and for the creation and customization of applications for both the Internet and intranets. Both Netscape and Sun Microsystems proposed to introduce JavaScript to the World Wide Web (WWW) consortium and to IETF (Internet Engineering Task Force) as an open Internet scripting language standard. Netscape and Sun hope to encourage the adoption of JavaScript as a widespread Internet standard by making it an open Internet standard, by giving a JavaScript license to Java licensees, and by providing a source code reference implementation of JavaScript.

JavaScript is an easy to learn scripting language designed to create dynamic online applications. These applications link together objects and resources on both the client and the server. Because there are different products for the client side and the server side of the equation, the following discussion reviews JavaScript, as implemented in Netscape Navigator versions 2.0 and 3.0. The focus then changes to the server side and LiveWire.

The Client-Side Features of JavaScript

Netscape created JavaScript as a scripting language for use within the <SCRIPT>...</SCRIPT> tags. JavaScript is a completely separate language from Java. Because it is a scripting language, the browser interprets the script after it loads the HTML document. The power of JavaScript derives from the following characteristics:

JavaScript Is Simple

Because JavaScript is a simple scripting language, the Web page developer can create a dynamic Web page without needing to become a programmer. At the same time, programmers can use it to implement Java applets within a Web page.

NOTE
JavaScript is not a programming language; it is a scripting language used within an HTML document. As a scripting language, JavaScript is interpreted and not compiled. Furthermore, its syntax enables a non-programmer to add functionality to his Web pages without first having to acquire a background in programming techniques

Every language has a role to play. The role of JavaScript is to provide a simple tool that extends the functionality of HTML. The complex tasks are left to plug-ins and Java applets.

JavaScript Is Dynamic

A Web page becomes dynamic when it responds to events generated by the user or other objects. With traditional HTML documents, server-side applications had the responsibility of handling events. JavaScript transfers event management to the client side. This makes Web page design more flexible, more dynamic, and more responsive. The trend in programming is to transfer more work to the client, leaving the server to handle the requests of more clients.

JavaScript Is Object-Based

JavaScript is not an object-oriented language like Java or C++, but it interacts with objects. An object-oriented programming language includes classes, inheritance, and strong variable typing, all of which JavaScript lacks. However, it does interact with objects that expose themselves to the scripting environment. JavaScript can, therefore, access objects in the Netscape browser (such as display characteristics or a URL). In Netscape Navigator 2.0, JavaScript can invoke plug-ins or applets. And now, with the release of version 3.0, it can communicate with plug-ins and applets. This capability to interact with different objects gives JavaScript a flexibility beyond that of more complex programs.

Java Versus JavaScript

The similarity of names reflects the commonality of Java and JavaScript. However, even though the JavaScript language resembles Java, it is not a simplified form of Java. JavaScript supports most of Java's expression, syntax, and flow control constructs, but does not support classes or inheritance. JavaScript supports a small number of dynamic data types (numeric, Boolean, and string) that are not typed, whereas Java supports a rich variety of data types that are static and have strong type checking.

JavaScript complements Java by enabling Web page developers to take advantage of the exposed properties of Java applets. JavaScript can get and set exposed properties, which enable it to alter the performance of both applets and plug-ins. Table 18.1 compares the features of JavaScript and Java.

Table 18.1. Comparison of Java and JavaScript.

JavaScriptJava
Interpreted by the clientCompiled before execution on client
Object-basedObject-oriented
Integrated with HTMLApplets are distinct applications
Variable types not declaredStrong typing of variables
Dynamic binding of object referencesStatic binding at compile time

New Features of JavaScript in Netscape Navigator 3.0

With the release of Netscape Navigator 3.0, the people at Netscape added several new features to JavaScript:

Netscape Navigator 3.0 includes LiveConnect, which enables

Web page designers can use LiveConnect to synchronize video and audio files. For example, you can use LiveConnect to make an event trigger an audible alert. With this enhanced communication, JavaScript could collect data from Java applets by accessing its public objects. With all the features of JavaScript, Web pages no longer need to be static documents.

The Server Side of JavaScript

On the server side of the equation, Netscape provides LiveWire for managing Web sites and creating server software. LiveWire consists of the LiveWire Site Manager, LiveWire JavaScript Compiler, and the LiveWire Database Connectivity Library. The JavaScript Compiler and the Database Connectivity Library are the most important features to the programmer.

Netscape servers include the capability to run compiled JavaScript applications. With this addition, the programmer can create platform-independent server applications. When combined with the Database Connectivity Library, server applications can make direct SQL connections to databases from Oracle, Sybase, Informics, and Illustra. The Database Connectivity Library also supports ODBC access to other databases. LiveWire provides an important dimension to applications development. While the server-side features of LiveWire extend the use of JavaScript, these chapters on JavaScript concentrate on the use of JavaScript in HTML documents.

When To Use JavaScript

Whenever you learn about a new language, you probably want to know the best uses for the language. JavaScript is still a young and maturing scripting language, so the following indicate potential uses of this new language:

With the advent of Java and JavaScript, the days of static HTML documents are over. A new era of interactive user participation, via the Internet and intranets, is now open.

The Limitations of JavaScript

As mentioned previously, JavaScript is a scripting language and not a full-blown, object-oriented programming language. JavaScript was not designed to build complex applications.

At the present, JavaScript exists only in Netscape Navigator, Netscape server products, and Microsoft Internet Explorer 3.0. Anyone designing a Web page for a wider audience must consider this in their Web page design. For example, although JavaScript can eliminate the necessity of using CGI scripts for forms verification, using JavaScript for this purpose would disable users of other Web browsers from forms validation. To design a Web page for a diverse audience, the Web page designer must write CGI scripts that determine whether or not editing is required according to the user's Web browser.

Unfortunately, the rapid growth of JavaScript created a problem with different versions implemented by different products. Netscape Navigator 2.x and LiveWire use JavaScript version 1.0. Netscape Navigator 3.0 implements JavaScript version 1.1. Microsoft Internet Explorer 3.0 adds another layer of confusion by using a variation of JavaScript 1.0 called Jscript. All these flavors of JavaScript means that the Web page developer must consider the intended audience when writing his scripts.

When JavaScript was first released, it enabled the sending of mail messages without user knowledge, the reading of the URL history, the reading of directories, and the reading of files. Although these features had valid purposes, hackers used them to gain unauthorized information about the user's system or network. Moreover, because JavaScript is part of an HTML document that flows freely through a firewall, it defeated the protection provided by firewalls that prevented unauthorized users from gaining this information.

In addition, Netscape Navigator 2.0 had a defect termed "stuck onLoad" that enabled a window created in the page to stay open even after the user exited the page. By creating a one-pixel by one-pixel window, hackers could continue to gather information about the user's system, network, or sites visited. Because the window would appear as a very small dot on the screen, the hacker could gather the information without the user knowing it. With the release of Netscape Navigator 3.0, Netscape closed all known security holes. Users of Netscape Nav-igator 2.0 need to upgrade to version 2.02 or Netscape Navigator 3.0 to close these potential security loopholes. Table 18.2 shows the various problems and the releases that fixed the problems.

Table 18.2. JavaScript security fixes.

Problem
Corrected in release
Capability to read directories
2.01
Capability to track history
2.02
The stuck onload defect
3.0b
Capability to send mail
2.01

With these loopholes closed, users can once again safely use JavaScript. However, they must upgrade to the most current release.

Summary

JavaScript is a new and powerful scripting language that enables Web page developers to write dynamic interactive documents with less reliance on writing server-side scripts. As an object-based language, JavaScript enables the developer to write Web pages that interact with Java applets and plug-ins. The door is now open to a new era of Web page development.