JAVA, Applets, and JavaScript

 

NOTE:  the ".class" file extension - DOS and Windows 3.1 do not support long filenames, and therefore cannot deal with the .class file extension (they truncate it to .cla).  But all modern OS's (Win95, 98, NT, 2000, XP, UNIX, LINUX, etc.) accept long filenames and therefore accept the .class file extension. 

Applets (Application-ettes)

The code starts with the term, "applet", with a pointer to where the code resides (codebase=path).  Applets rely on "class" JAVA files, which are executables - they are compiled JAVA source code.  An applet is self-enclosed and this is where the functionality resides completely.

An example of a class file would be "snow.class", which when called on to execute by the applet, will apply dropping snowflakes onto an image.  The applet code must give the name of the class file, and must give the name of the image file, with the appropriate paths, of course:

<APPLET CODE=snow WIDTH=400 HEIGHT=647>

<PARAM NAME=image VALUE="snowgirl.jpg">

</APPLET>

 

In this example, the browser knows that the file extension of "snow" must be "class", and appends it before executing the file.  Alternatively, you could use:

 

<APPLET CODE=snow.class WIDTH=400 HEIGHT=647>

<PARAM NAME=image VALUE="snowgirl.jpg">

</APPLET>

 

JAVA vs JAVASCRIPT

JAVA is the executable code that your browser's JAVA VM (Virtual Machine) runs. It uses "class" files to contain the code. 

JAVASCRIPT is a much higher level language consisting of simple text - it is executed without compilation.  The text is interpreted by your browser and the commands are executed by a "JavaScript engine" contained within your browser. It can be contained within HTML code, or "js" files.  JAVAScript can also make calls to JAVA class files, and then they will be executed in the JAVA VM engine - so your browser may be running two sets of code as it processes JAVAScript.

As you can see, JAVAScript does not necessarily require any JAVA "class" files.  

You will hear repeatedly that javascript is not JAVA - and that javascript is has nothing to do with JAVA - which is pure BS.  They say that simply because it is not compiled and therefore is not pure JAVA.  However, it is interpreted by a software engine that replicates JAVA processes . . . therefore:

JAVA Script has everything to do with JAVA.  That's why it is called "JAVA" Script !!!

True it is not the same thing as JAVA - but they work hand-in-hand with one another.  JAVA is the programming language, and javascript is a much higher-level language that may or may not make calls to JAVA class files.  Even if it does not use any class files - the language was developed as a sort of simplified JAVA, and was based on JAVA.  It is simply that - a script that tells the browser what to do, what JAVA class files to access if any, where they are, and a set of parameters to use.

Here is an example of JAVA and JAVASCRIPT - both doing the same thing, displaying "Hello World" :

JAVA

import java.awt.Graphics;



public class HelloWorldApplet extends java.applet.Applet {



	public void paint(Graphics g) {

		g.drawString("Hello world!", 5, 25);



	}

}

JAVASCRIPT

<script language="javascript">

document.write ("Hello World!")

</SCRIPT>

There are one or two parts to JAVA Script - the actual script statements, and then the optional calls to that script.  The initial script code comes "before" the <Body> portion of the web page, and is enclosed within a header and trailer - it begins and ends as follows :

<Script Type=  . . . >        OR        <Script language=  . . . >

    .  .  .  .

    declare variables
    preload images

    define functions
    code

    .  .  .  .

    .  .  .  .

</Script>

<Body>

    .  .  .  .

    calls to the script

    .  .  .  .

</Body>

 

The Script code statements declares variables, such as i=1, defines functions (subroutines), and includes parameter statements to define size, speed, etc.  It can also preload images, either for that page, or for subsequent pages that are linked to that page (very useful !!!).  By preloading images, for example - a rollover - when the user moves his mouse over the base image, the swap image immediately appears.  Without preload, the mouseover image must be downloaded - which eliminates the effect, because no one will wait while the swap image downloads.  

The initial script definition statements come "before" the Body of your web page.  Within the Body of your web page, is where you place "calls" to the functions, variables, and parameters that are now defined for you.

 

Applets vs JAVAScript

Applets and Script are basically the same, functionally.  Applets are little snippets of code that are portable because the code contains everything you need.  They support drag and drop of the "plugin" that shows up in your web development application.  Scripts are sometimes more long and complex than applets - and they are "usually" but not always, two parts - the Script statements that define functions and declare variables, etc - and then subsequent calls to those functions.

Samples for Comparison

JAVA Applet (executes the file, "snow.class")

 

<APPLET CODE=snow WIDTH=400 HEIGHT=647>

<PARAM NAME=image VALUE="snowgirl.jpg">

</APPLET>

 

JAVA Applet (executes the file, "LED.class")

 

<applet codebase="../LED/" code="LED.class" width=500 height=48 align=center>

<param name="script" value="lifeson.led">

<param name="border" value="2">

<param name="bordercolor" value="100,130,130">

<param name="spacewidth" value="3">

<param name="wth" value="122">

<param name="ht" value="9">

<param name="font" value="default.font">

<param name="ledsize" value="3">

</applet>

 

JAVA Script

 

<script type="text/javascript">
function init() {

  if (!document.getElementById) return

  var imgOriginSrc;

  var imgTemp = new Array();

  var imgarr = document.getElementsByTagName('img');

  for (var i = 0; i < imgarr.length; i++) {

    if (imgarr[i].getAttribute('hsrc')) {

        imgTemp[i] = new Image();

        imgTemp[i].src = imgarr[i].getAttribute('hsrc');

        imgarr[i].onmouseover = function() {

            imgOriginSrc = this.getAttribute('src');

            this.setAttribute('src',this.getAttribute('hsrc'))

        }

        imgarr[i].onmouseout = function() {

            this.setAttribute('src',imgOriginSrc)

        }

    }

  }

}

onload=init;

</script>



<Body>
<img src="button1.jpg" hsrc="button2.jpg" width="160" height="35"><br>

</Body>