enables Web pages to be interactive and intelligent, and can add
excitement to an otherwise dreary page.
If you've worked with HTML-the language of the Web-and have a
basic idea of the concepts of programming, you should have no
its features, and its limitations.
Java. Java is a new kind of Web programming language developed
by Sun Microsystems. A Java program, or applet, can be
loaded by an HTML page and executed by the Java Interpreter, which
is embedded into the browser.
Java is a complex language, similar to C++. Java is object-oriented
and has a wide variety of capabilities; it's also a bit confusing
languages. These are simple languages that can be used to
add extra features to an otherwise dull and dreary Web page. While
Java is intended for programmers, scripting languages make it
easy for nonprogrammers to improve a Web page.
use in its browser, Netscape Navigator. It includes a convenient
syntax, flexible variable types, and easy access to the browser's
features. It can run on the browser without being compiled; the
source code can be placed directly into a Web page.
compilers are required. You can use the same editor you use to
on the browser (currently, Netscape or Microsoft Internet Explorer).
by Sun, the developer of Java, as a scripting language to complement
Java. Support has also been announced by several other companies.
can be quite useful in its own right. It can work directly with
HTML elements in a Web page, something Java can't handle. It is
also simple to use, and you can do quite a bit with just a few
throughout this guide.
Java was originally developed by Sun Microsystems for use in "real-time
embedded systems"-in other words, consumer electronics. Java
has now become the de facto standard for advanced Internet programming,
but you may still see it running your cellular phone someday.
Java was designed to operate on a virtual machine-a piece
of software that interprets the Java code and acts on it as if
it were a computer in itself. The virtual machine was designed
to be simple so it could be implemented easily in a device. This
is what makes it easy to implement in Web browsers.
Several companies, including Oracle and Apple, have proposed designs for network computers-simplified pcs designed to work online, getting their software over the net. Most of these machines will use Java, or a variation of it, as an operating
Java was originally supported only by HotJava, an experimental
Web browser developed by Sun for that purpose. Recognizing its
potential, Netscape integrated it into its Web browser, Netscape
Navigator. Because Navigator is the most popular browser, this
greatly increased the publicity for Java.
In 1995, Java became the hottest new buzzword for the Internet,
but few people actually knew how to program it. Netscape Communications
recognized the need for a simple, clear language for Web pages
and introduced LiveScript, the first of the Web scripting languages.
LiveScript had a syntax based on Java, but was more concise and
easier to learn. It was also a directly interpreted language rather
than a compiled language like Java. Netscape built LiveScript
into the beta versions of Netscape Navigator. Support for LiveScript
began with version 2.0b1, released in June 1995.
Later in 1995, Netscape reached an agreement with Sun. Sun also
recognized that a simple scripting language was a good idea, so
they officially endorsed LiveScript. Thus, the connection with
Java became official, and the name changed to the one you're familiar
finalized by the end of 1996, and other companies-most notably,
Microsoft-are rushing to release competing products. Microsoft
Internet Explorer (MSIE) 3.0, currently in beta, supports basic
The process of writing a Java applet can be complicated-writing
the source code, compiling, and making the applet available on
by the browser. The source can either be included directly within
an HTML page or referenced in a separate file.
Java, it adds some conveniences, simplifies programming, and provides
better integration with the browser. The key differences include
the following (explained in the following sections):
Java applets are compiled and stored on the server as byte codes,
an HTML page.
The <SCRIPT> tag, an
extension of HTML supported by Netscape, enables you to include
take a more detailed look at the syntax of these tags later in
an HTML document.
HTML Text goes here.
document.write("Here is my output.")
An alternate option called an event handler enables you
event occurs. For example, a button on the page might have an
action performed when it is pressed. This provides a more dynamic
Instead of the <SCRIPT>
tag, an event handler is added as an attribute to an HTML tag.
As an example, the following HTML code defines a link with an
<A HREF="www.netscape.com" onClick="alert('This will take you to Netscape's home page.');">
In this case, the name of the event is onClick.
This particular event happens when the user clicks on the link.
within double quotation marks.
than that of Java, and variables (names used to store values)
are easier to use. Here are the specifics:
Changing a script is as simple as changing an HTML page.
- Rather than creating objects and classes, you can do quite
- Variables are loosely typed: You do not need to declare
variables before their use, and most conversions (such as numeric
to string) are handled automatically.
an action is performed on part of the HTML document. For example,
a form's input field can be checked for certain values whenever
it is modified.
means that it can use objects. An object is a more complicated
version of a variable. It can store multiple values and can also
to represent just about anything.
of the browser directly. These objects represent actual elements
of the browser and the Web page, such as windows, documents, frames,
forms, links, and anchors.
You can access information about the links, anchors, and form
elements in the current page. You can also control the browser
button on your page that will send the user back to the previous
page-just like the browser's back-arrow button.
Of course, when I say "browser" here, I'm talking about
it in version 2.0b1, and at this writing, version 3.0b6 is the
latest version. It will also be supported by Microsoft Internet
platforms, it is supported more widely than Java. Until Java becomes
enables you to reach a wider audience on a wide variety of platforms.
Netscape Navigator. This functionality is provided on the following
- Windows 3.1, using the 16-bit version of Navigator
- Windows 95 or Windows NT, using the 32-bit version of Navigator
- Macintosh System 7 or higher, using the Macintosh version
- Most versions of UNIX, using the X Window version of Navigator
Although this is a wide range of support and should enable about
90 percent of the Web's audience to use your script, but don't
forget the other 10 percent. Whenever possible, be sure to provide
for further information.
important, but what's it really good for? What will it do for
your Web pages? The following sections present some of the most
some of the text dynamic. As a simple example, you could display
an appropriate greeting-"good morning"
or "good evening",
depending on the time. Because you can use and control the HTML
itself, you can extend this by dynamically choosing graphics or
or any factor, you can present the appropriate page.
You've seen forms at many Web pages, and probably used them yourself.
The user enters data into the form, then presses the Submit button,
and the server, using a CGI program, responds to the information.
This is useful, but it isn't very interactive-before you can receive
a response, you have to wait for the data to travel to the server
can't send the data to the server until the Submit button is pressed,
you could fill out a loan application online and, as you fill
it out, instantly get feedback about the kind of payments the
loan will require. After you're satisfied with the application,
you can then submit it to the server.
from a normal Web page by giving the user some control of the
page. For example, you could have a background on/off button to
control the display of the background, or a button to toggle the
display of a table of contents.
The following sections introduce the tools you need to create
a script with an HTML page. Finally, you'll learn about the hardware
Currently, the only requirement in this area (and the only option)
is Netscape Navigator, version 2.0b or higher. The available hardware
platforms are discussed later in this section. Several other browser
Provided you have a version of Netscape to view your creations
on, there are no tools that are specifically required in order
is a text or HTML editor-probably the same one that you use to
develop HTML pages.
much more complicated. Take a look at the simple example in Listing
Listing 1.2. (ALERT.asp) A script that displays a message in
the text and in an alert box.
This example displays the "Hello!"
message as part of the HTML document. In addition, it uses the
alert() statement to display
a message to the user in a dialog box.
What happens when you load this Web page in Netscape? Let's take
what happens and in what order, you'll find it easy to learn the
Let's start with a review of the basics: What happens when you
request a Web page from a server? This is an important process
to understand. Here are the steps involved:
- You enter a URL into your browser, or select a guidemark.
- The browser sends an HTTP request for the URL to the appropriate
- The server sends back the contents of the Web page at the
- The browser sends additional requests for each of the graphics
- After receiving enough information about the graphics to devote
the correct amount of space to them, the browser displays the
- The remaining graphics are displayed as they are received.
After the full HTML document is received, as in step 3 in the
last section, it is examined for the <SCRIPT>
tag. If a script is included, it is processed; however, the processing
depends on the type of script:
- If the script is included in the header, it is ignored unless
a script later calls it.
- If the script is included directly in the body, its output
will be included in the Web page-thus, it can affect the display
of the page.
- If the script is an event handler for a specific part of the
page, it will be processed only when the event happens.
program; you choose one of these methods depending on your needs.
In this section, you learn some of the problems you might encounter
might affect the security of your source code and data, you should
about security-most of them legitimate. The Computer Incident
Advisory Committee (CIAC), an agency of the Department of Energy
that monitors computer security problems, has reported several
major problems-such as a method for a wayward script to erase
your hard drive. All the potential problems involve the owner
of the script or Web page being able to access information on
your system. Here are the incidents the CIAC has reported:
to the remote site.
- The browser's disk cache can be read and transmitted.
- Your e-mail address can be read-and used to send an e-mail
without your knowledge.
- A list of files on your hard disk can be obtained.
- URLs you visit can be logged by a script and transmitted.
All these problems are specific to Netscape's implementation of
later versions of Netscape. The first two were fixed by version
2.0; the remaining ones are fixed by versions 2.01 and 2.02.
If you have the latest version of Netscape, you should be safe
from these problems. However, considering that this many problems
have been discovered, there is always a chance there will be more.
source available to anyone who wishes to copy it.
This means that you might spend hours working on a script, only
to find that people have copied it onto their own Web pages. You
for pay or specifically for your own Web page.
Remember that even though your source code is accessible, that
doesn't mean it's free to be copied. You legally have a copyright
on any script you write.
If someone does copy your scripts, often the best solution is
to send that person a friendly note pointing out your copyright
and saying that you do not want anyone else using it. You should
also clearly state this in the source code and on the Web page
that contains it.
If you've developed many HTML pages, you will recognize that HTML itself has this same problem. Although there's usually no harm in copying an HTML technique from a Web page-such as using <UL> for a list-some unscrupulous users have copied
perfect and has many limitations. Some of these may be fixed as
is meant to be an easily understood scripting language, some features
just don't belong.
These include programming a solution "the long way,"
Java, CGI, and SSI.
missing features. These might be fixed by a future version, but
some may never be fixed. The following sections describe some
Working with Entire HTML Pages
example, displaying the current time or date or including information
that depends on the user's input. Unfortunately, there is no way
HTML page. You can reload the page with different contents, but
you can't change the text on a page in place.
There are some elements of the page you can change: the images,
and anything in a form element. You can also display the information
you need to change in a separate frame. You can also get around
this by using Java, which has no such limitations.
Lack of Network Communication
browser and the HTTP server. This means that although you can
can't send the resulting data back to the server. You can combine
is the easiest way to do this.
Limited Graphics and Multimedia Capabilities
it to be a multimedia powerhouse. You can include graphics in
create graphics-for example, draw a graph. Currently, Java is
the best solution when you need to do this.
Limited Size of Scripts
required that the scripts you use for a Web page be included in
the HTML for that page. This means that there was a practical
limitation of about 32K for the page and all scripts, because
the browser must download the entire page before executing the
attribute to the <SCRIPT>
tag, and this is supported by Netscape's latest version. This
to an image. This should make much larger scripts practical. To
use it, follow these steps:
such as program.js.
- Include the <SCRIPT>
tag to refer to it, such as <SCRIPT
directory on the Web server.
calculation or manipulation of HTML tends to run at a slow creep.
I assume that Netscape is aware of this and will improve the speed
in future versions. Unfortunately, complex Java applets also run
quite slowly, so there is no clear solution at this time.
incredibly fast. It's only complex applications that suffer from
you've learned, there are some where it doesn't even come close.
solution is to use the right tool for the job. Several Web languages
Java includes many features, including communication across the
network and graphics capabilities.
to learn it. Many ready-to-use Java applets are available. You
will find several of these on the CD that comes with this guide.
tightly integrated with Java. You can use a Java applet and use
of this technique.
Common Gateway Interface (CGI) has been a standard almost as long
as the Web has, and it can be used to make Web pages interactive.
You encounter CGI all over the Web-anywhere you fill in a form
and press a Submit button, you've accessed a CGI script.
CGI is not actually a language, but rather a standard interface.
You can write CGI programs in any language. The most commonly
used languages for the task are Perl and C.
on the server. Your client sends information to it in the request
and then receives the results in the form of a Web page. You cannot
have instant results-such as a total that automatically updates
gives you the best of both worlds.
An alternative to CGI is Server-Side Include, or SSI. This enables
capability, but it happens on the server side.
Like CGI, SSI is a standard, and you can use any language to program
in the page based on certain factors. You'll find examples of
this in Chapters 17 and 19.
Unfortunately, although most Web servers support SSI, not all
systems (or system administrators) allow their use. The main reason
for this is that each Web page has to be parsed for SSI instructions,
which slows down the server. There are also security concerns.
Ask your system administrator whether you are allowed to use SSI.
do much, but it will help you understand the steps required to
develop and test a script. You'll find much more sophisticated
examples throughout this guide.
following script simply displays the location of the current page
and a brief message. This script will be combined with an HTML
page and its use demonstrated.
document.write("<B> location: </B>" + document.location + "<br>")
After you've created the script, you need to do two things:
- Embed the script in the HTML page. You can use the <SCRIPT>
tag to do this, or use an event handler.
- Test the script by viewing the document with Netscape.
application, you'll end up using both of these methods several
Using the <SCRIPT>
page is to use the <SCRIPT>
tag, as described earlier in this chapter. This tag is usually
used as a container, and the script is included directly after
it. Listing 1.3 adds the necessary opening and closing <SCRIPT>
tags to the script:
Listing 1.3. A simple example of the <SCRIPT>
document.write("<B> location: </B>" + document.location + "<br>")
Notice the strange syntax. The extra brackets and exclamation
marks indicate a comment; the entire script is marked as a comment
browsers will execute it correctly.
If you use this method within the body of a Web page, the script
will be executed immediately when the page loads, and the output
of the script will be included at that point in the page. You
can also use the <SCRIPT>
tag within the header of a page to prevent it from executing immediately.
This can be useful for subroutines that you will call later.
An alternate approach is to use an event handler to perform a
script when a certain event occurs. This is best used when you
want to act on the press of a button or the entry of a field.
Rather than use the <SCRIPT>
tag, an event handler is inserted as an attribute to an HTML tag.
Tags that support event handlers include <LINK>,
<IMG>, and the form
As a basic example of an event handler, here's a common use for
like the browser's back button. You can easily accomplish this
with an event handler, as in Listing 1.4.
<INPUT TYPE="button" VALUE="Back!" onClick="history.go(-1); return true;">
This defines a button with an event handler. The event handler
is defined as an attribute of the <INPUT>
tag. The attribute name is the event name-in this case, onClick.
This is an event that occurs when the user clicks the mouse on
In this example, a button is used to send the user back to the
previous page. You could also use this technique with an image,
or a simple link to the word "back!".
Because an event handler is inserted between double quotation marks, be sure to use single quotation marks to delimit any strings within the event handler.
The main tool you'll use to view the script's output is a Web
browser. Currently, you should use Netscape to view the output,
nothing special you need to do to view a script's output-just
load the Web page that contains the script. You can even test
to the Web server.
Navigator, although they may work with older versions.
browsers; however, the last thing you want is for someone using
an ancient version of Mosaic to load your page and see the actual
of a <SCRIPT> tag with
document.write("I lost a buttonhole. ");
The key elements here are the comment begin and end tags: <!!
and ->. These are standard
HTML 2.0, and define the start and end of a comment.
Unfortunately, things aren't always that simple. Here are a few
things to watch out for:
- Some browsers will treat any greater-than sign (>)
as the end of the comment. The decrement operator (-)
can also cause problems.
- Some browsers still won't recognize comments correctly, so
your script may be displayed.
Because of problems like these, there is no ideal solution. The
best way to avoid these problems is to use the <SCRIPT
SRC> tag instead, although it isn't always the
best solution. Another solution is the <NOSCRIPT>
For clarity, I won't use HTML comments in the examples through
this guide. If you use these techniques on a Web page of your own,
you may wish to add comments to support older browsers.
powerful tool to enhance a Web page. It offers features that are
available to no other Web language. You've learned the following
terms and concepts in this chapter:
and keep it from displaying on older browsers
with Larger Programs and Variables."
turn to Chapter 3 "Working with Objects and Events."
- To learn to manipulate and use parts of the Web page with
Chapter 7 "Real-Life Examples I."
- To learn more about Java, turn to Chapter 16, "Integrating
|A:||It all comes down to marketing. Java was a publicity phenomenon at the time, and simply including the word Java (or any other coffee-related word) in a product's name was enough to
get instant attention. Netscape got permission from Sun to do so, and the rest was history. Unfortunately, this has caused many beginners-and many columnists-to consider them identical, or at least much more similar than they really are.
|A:||Yes. Although the languages have significant differences, many of the basic structures, functions, and concepts are the same. Be sure not to assume that things will work exactly the
Java-they rarely do.