Home -> Workshops -> NJ Library Association

JavaScript and Java - Basic Information

Please click here for a PowerPoint Presentation given to the NJ Library Association

Or, read further for some minuscule notes on JavaScript and Java, including some Examples of JavaScript and Examples of Java Applets.

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.

BUT

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 (but you could put a link to such documents into a 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,

BUT

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.
Example:
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 FormMail.pl which will simply email the responses from a form to a specific email address. The recipient's address and the subject 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
interactive 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, they 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 JavaScript and simple 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, however, somewhat limited in their abilities but almost all "nice" web pages use them to improve their look and feel.

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.

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 can also (optionally) hide the JavaScript code from non-JavaScript aware browsers by using comments, as in this example:

<HTML>
<HEAD>
<TITLE>Title of document</TITLE>
<SCRIPT>
<!-- Start of script statements
function Welcome()
{  document.writeln("Welcome to Java Script");
}
<!-- End of script statements -->
</SCRIPT>
</HEAD>
<BODY>
<H1>
<SCRIPT>
<!-- 
   Welcome()
<!--  -->
</SCRIPT>
</H1>
</BODY>
</HTML>

JavaScript Examples:

Click the link on the left to see the code in action, and the 'Explain' link for some brief explanation. 

NOTE: All examples work with Netscape 4.5 or above, and probably with 4.06 and above. If you are using another web browser, and things do not work as advertised, first download Netscape 4.6 and try again - it should work if you are using that web browser ....

Status info displayed over links (watch the status bar at the bottom while the mouse hovers over the previous and next link)
Explain
Here is a "regular" link to a particular page.
Explain
Here is a "regular" link to a page but a message pops up before the link is followed
Explain
Here is a page with a lot of links, and here is the same page with a selection instead, or even better, with "fixed" selection menu using frames
Explain
Here is a "regular" link to a page but the user must first enter a password before the link is followed. The password, in this case, is xyzzy but it's not very secret (View | Source would reveal it).
Explain
Here is a another password-protected page: click here for secret page. This time, try to see if you can "find" the password; it's more difficult now, but not impossible (View | Source would not reveal it, but gives you a clue). Passwords based on JavaScript code are never really secure, but they might serve to make a page hard to get to for 90% of all web users. To really protect a page, or pages, you need to contact the web site administrator, he/she can do that in a secure fashion.
Explain
Here is a link that brings up a new window.
Explain
Here is a link that brings up a new window of specified size without menus and such, not resizable.
Explain
Here is a page showing "user info", as best as possible.
Explain
Working with forms (see last two slides of the presentation)
Explain
Some dynamic pages (clocks, tickers, slideshow).
Explain
This example creates a simple Web page and puts the current date and time into the page.
Explain
This example displays different greetings, depending on the time of day.
Explain
Take an on-line quiz that counts the number of tries and correct answers.
Explain
See how HTML codes will change the looks of your text
Explain
GPA Calculator or Tax Form Calculator (1040EZ)
Explain
Have a help window available
Explain
Roll-Over images (images that change when the mouse hovers over them)
Explain
Extensive online quizzing for German Language
(click on the "Kapitel" (= chapter) links, you'll see examples soon ...)
No Explanation

For further (extensive with plenty of detailed examples) information on JavaScript, please check

The JavaScript Guide
The JavaScript Reference Manual

Java

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.

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.

To create Java applets is, generally, difficult. To use Java applets, on the other hand, is very easy - if you find an existing one that will serve your purpose. In any case, you need a Java aware web browser to see them in action:

Netscape 3.0 and above:
JavaScript and Java aware on any platform, but there are different versions ...
Internet Explorer 4
JavaScript and Java aware on any platform, but there are different versions ... also, JavaScript code does not necessarily run the same on IE and Netscape
HotJava:
a special Java-aware browser for Windows 95 and Unix systems, fully Java aware
Any other browser:
most are "a little" JavaScript but not Java aware - but then, Netscape and IE cover most of the useful web browsers anyway ...

The public-domain browser that started the whole Web hype, NCSA Mosaic, may never become Java aware - unfortunately.

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)
or
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">
<...>
</APPLET>

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.

Example:
This example shows a 3-D model of a chemical structure that can be rotated in space - once loaded, drag the mouse over a molecule to rotate it. A second example is here.
Additional Examples:
Bert's Sample Java Page (ever-expanding)
Other sites with Java Applets (many for available for download)
http://www.jars.com/
 

Bert G. Wachsmut
Last modified: 05/03/00