Part 1: Basic Info and Examples

The Need for Something New
What is Java Script and Java Applets
Who can create Java Script and Applets, and Who Can Use Them
How to use Java Script and Java Applets
Examples of Java Script
Examples of Java Applets
Writing Your Own Java Script-enhanced Web Pages

The Need for Something New

Web pages can incorporate formatted text and graphics elements to present information in an esthetically pleasing form. You can even enhance the information content by adding hyperlinks to more information, or to other types of documents.


Web pages are static, i.e. the content does not change over time (no animation possible)
Web pages are not interactive, i.e. the content does not change depending on the user's action
Links to other types of documents require so-called helper applications that may or may not be installed at the user's computer
The types of documents that can be accessed is limited, i.e. you can not include a Lotus 123, an SPSS, or a DBASE file in a Web page

To overcome these deficiencies, a mechanism called CGI Scripts is usually used:

CGI (Common Gateway Interface) scripts are programs that are installed on the Web server - not the web client - and that can process information that a Web client such as Netscape gives them. Almost all forms, for example, are filled out on the client, and then submitted to a server, where a CGI script processes the information and delivers an appropriate response. A very common mechanism is a Search, where the user enter some key phrases, the CGI script searches a database for appropriate responses, and delivers those back to the user's client program.

CGI scripts can overcome the first two problems,


CGI scripts do not provide helper applications to a client computer
CGI scripts are usually machine-dependent, i.e. if a script can perform a task on one machine (say a SUN workstation) the same script may or may not work correctly on another system (say a PC running a Windows Web server program)
CGI scripts are processed at the server. If many people use a particular server, it may become pretty slow, lowering overall performance even for those people who do not use the CGI scripts.
Not everyone can create CGI scripts. In fact, usually only people with special accounts on the server machine can write CGI scripts. As an example, on our machine only Andrew Pu and myself can write CGI scripts. This restriction is necessary for security reasons.
As an example, anyone who has a homepage on our SHU system can create a form for user comments or input. Anyone can use a CGI script called mailin which will simply email the responses from a form to a specific email address. The recipient's address can be hidden in the form itself. But, the creator of the form can not control anything else, such as the exact format of the email message, or the content of the information that is displayed back to the user who submitted the form.

What is Java Script and Java Applets

Java Script and Java Applets are convenient methods to provide
animation and content that changes over time
interative documents that change depending on a user's response
links to new types of documents without the need of a helper application
ability to process any type of document, without need of a helper application

In addition, the provide advantages over CGI scripts, since:
Java is machine-independent, i.e. it runs on IBM, Macintosh, and Unix machine without modifications
Anyone can create Java Scripts and Java applets, without special privileges
Java Script programs are reasonably easy to create
Java is its own, full, object-oriented programming language and provides many, many more features than are possible with CGI scripts
Java Scripts and Applets run on the client machine, keeping the load off the server

Java Script

Java Script is a simple, object-oriented, interpreted programming language that provides methods (or functions), commands, and event handlers that can be embedded right into an HTML document. You do not need to compile your commands, and you do not need any special programs to create Java Script programs. Java Script programs run on any machine using a Java Script-aware browser such as Netscape. Java Script programs are limited in their abilities, and they since they are interpreted line by line, they are slow.

This example creates a simple Web page and puts the current date and time into the page.
This example displays different greetings, depending on the time of day.


Java is an object-oriented, very complete, machine independend programming language that can be used to create either stand-alone programs or programs that are embedded in a Java-aware Web browser (such programs are then called applets). The Java programs need to be compiled before they can be used, and you need a special Java compiler to create the Java programs. Java programs run on any machine as standalone programs, complete with Graphical User Interface, or they can be embedded into a Java-aware browser such as Netscape. Java programs can incorporate almost any features, including graphics, menu, and networking features, that are expected of any other Windows program, and since it is a compiled language, it runs fast. To write a Java program or applet, you must create the source code in a *.java file and then compile it into a byte-code file called *.class. The class file then contains the runable applet or program.

This example shows an animated 'Under Construction' sign
This example shows a 3-D model of a chemical structure that can be rotated in space, using the mouse - a second example here.

Who can create Java, who can use it

Creating JavaScript

Anybody who can create HTML pages can also create Java Script programs embedded in an HTML document. No special privileges are needed, and no special software or hardware needs to be used.

If you place your Java Script enhanced HTML document on a web server, anybody can access that page and run the JavaScript program, if they are using a JavaScript-aware browser.

Creating Java Applets

In theory, anybody can also create Java programs, which could be either embedded into an HTML document, or distributed as a stand-alone program for any type of computer. However, it is not trivial to create Java programs, and you need at least:
Programming knowledge (object-oriented variety preferred)
Windows 95 or brand of UNIX
The Java Development Kit (contains Java compiler)
Documentation about available Java classes

If you created a standalone Java program, anybody could use it (if they can access it). If you created a Java applet (a Java program embedded into a Web page), and you placed the program and the corresponding page on a Web server, then anybody who has a Java-aware browser can use your program without further setup.

Using JavaScript and Java Applets

To use JavaScript, you could located already existing examples, but generally you will write JavaScript functions yourself, at the same time you create an HTML document. Writing Java Script program is probably easier than finding one tha works for you.

To use Java applets, on the other hand, is very easy - if you find an existing one that will serve your purpose. To write the Java applet yourself, on the other hand, is not at all easy, and you are better of to try to locate an existing applet that can do what you want.

In any case, you need a JavaScript and/or Java aware web browser to see them in action:

Netscape 1.1 and below:
not Java aware
Netscape 2.0 and above:
JavaScript aware on any platform
Java aware for Macintosh, Windows 95, and Unix
a special Java-aware browser for Windows 95 and Unix systems, Java aware (with restrictions)
Any other browser:
neither JavaScript nor Java aware

Microsoft has licensed Java technology from SUN, and the future versions of their Internet Explorer will certainly be Java aware, and very likely JavaScript aware. Other browser will probably follow, although they need to license the technology from SUN. Thus, the public-domain browser that started the whole Web hype, NCSA Mosaic, may never become Java aware - unfortunately.

How to use Java Script and Java Applets

To define JavaScript functions that can be used inside an HTML document you usually place special code between the <HEAD> and the </HEAD> tags at the top of the page. You should also hide the JavaScript code from non-JavaScript aware browsers by using comments, as in this example:

<TITLE>Title of document</TITLE>
<SCRIPT language="JavaScript">
<!-- Start of script statements
function Welcome()
   document.writeln("Welcome to Java Script");
<!-- End of script statements -->
<!--  -->

Now we want to get into the details of creating JavaScript-enhanced HTML pages and using existing Java applets as guides. Here are, therefore, a few examples that we will analyze. We can take a look at the HTML source to see how they actually work.

Examples of Java Script

This example creates a simple Web page and puts the current date and time into a the page.
This example displays different greetings, depending on the time of day.
Take an on-line quiz that counts the number of tries and correct answers.
See how HTML codes will change the looks of your text
Have an online calculator ready, just in case.
GPA Calculator or Tax Form Calculator (1040EZ)
Have a help window available
An automatic slide show

Examples of Java Applets

To use Java applets in a page you need to have:
An applet in byte-code (i.e. with .class extension)
An applet in source code (.java) and the Java compiler
Documentation about the applet, stating the name and meaning of any parameters the applet can take

To use the applet, simply put the <APPLET> and the </APPLET> tag together with the appropriate parameters into your HTML document and the applet will run at the indicated spot.

The Applet tag has the following form:

<APPLET CODEBASE="basedir" CODE="filename.class" WIDTH="xxx" HEIGHT="xxx">
<PARAM NAME="param_name" VALUE="param_value">
<PARAM NAME="param_name" VALUE="param_value">

For non-Java aware browsers, you can put any standard HTML code and/or tags between the APPLET tags. It will be ignored by the Java-aware browser, while the non-Java aware browser will display it and ignore the APPLET and PARAM tags.

This example shows an animated 'Under Construction' sign
This example shows a 3-D model of a chemical structure that can be rotated in space, using the mouse - a second example here.
The Java and Java-script enhanced ad for this workshop (notice the clock at the bottom of the page)
Seton Hall's Home Page
Self-running slide show with combined audio
Slide show with control panels and fading images

Writing Java Script-enhanced Web Pages

Here is Netscape Developer's Site , which supposedly contains the latest information on using JavaScript programming and plugins.

(Bert G. Wachsmuth)