HELP! * YELLOW=global GREY=local Full HTML for

GLOBAL foilset Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples

Given by Geoffrey Fox at CPS616 spring 1997 on Feb 12 1997. Foils prepared 21 February 97
Abstract * Foil Index for this file Secs 82 See also color IMAGE

This discusses in detail use of JavaScript in forms (including event handler examples) and paramterized HTML
Then it covers LiveWire, JavaScript URLs, JavaScript entities
The formal structure of <SCRIPT> tag
Comparison of Java and JavaScript
Start of Language Discussion

Table of Contents for full HTML of Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples


1 CPS 616 JavaScript Lectures
with Audio February 12 97
Basic JavaScript Functionalities and Examples
See:
http://www.npac.syr.edu/users/gcf/cps616-97feb12

2 Abstract of CPS616-97 Lecture of February 12
3 Hello World Example of JavaScript- I
4 Hello World Example of JavaScript- II
5 Text attributes
6 Background color(the only HTML color that may be changed on-the-fly is the background color)
7 Stanford Background Color Selector
8 Example of Clicking on a Form - I
9 Example of Clicking on a Form - II
10 Example of Clicking on a Form - I
11 alertmethod (this method is useful for debugging)
12 onunload event handler
13 Example of Parameterized HTML
14 Embedding Images in the Document
15 Example of Parameterized HTML
16 LiveWire -- Netscape's Approach to CGI
17 Refinements in the <SCRIPT> Tag - I
18 Refinements in the <SCRIPT> Tag - II
19 JavaScript URL's
20 Interpreted JavaScript
21 JavaScript URL's
22 JavaScript Entities in HTML
23 JavaScript Entity
24 JavaScript Compared to Java - I
25 JavaScript Compared to Java - II
26 The JavaScript Language -- Values, Names Etc.
27 simple addition

This table of Contents Abstract



HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 1 CPS 616 JavaScript Lectures
with Audio February 12 97
Basic JavaScript Functionalities and Examples
See:
http://www.npac.syr.edu/users/gcf/cps616-97feb12

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * See also color IMAGE
Secs 84.9 Full HTML Index
Instructor: Nancy McCracken
teamed with Meryem Ispirli, Geoffrey Fox,
Tom Scavo, John Yip
Syracuse University
111 College Place
Syracuse
New York 13244-4100

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 2 Abstract of CPS616-97 Lecture of February 12

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * See also color IMAGE
Secs 82 Full HTML Index
This discusses in detail use of JavaScript in forms (including event handler examples) and paramterized HTML
Then it covers LiveWire, JavaScript URLs, JavaScript entities
The formal structure of <SCRIPT> tag
Comparison of Java and JavaScript
Start of Language Discussion

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 3 Hello World Example of JavaScript- I

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 201.6 Full HTML Index
<HTML>
<HEAD>
<Title>A Test of JavaScript</Title>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript" >
<!-- A Conventional comment to hide JavaScript from old browsers
document.writeln("<h1>Hello World!</h1>");
//scriptend-->
</SCRIPT>
<b>Continue with conventional HTML</b>
</BODY></HTML>

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 4 Hello World Example of JavaScript- II

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * See also color IMAGE
Secs 289.4 Addon Full HTML Index
There is only one real JavaScript statement here -- namely
document.writeln("textstring");
This outputs into current page the text in quotes followed by a newline
And note the rather peculiar way we "hide" JavaScript from browsers that can't understand it by enclosing in convential HTML Comment syntax
<SCRIPT LANGUAGE="JavaScript" >
<!-- A Conventional comment to hide JavaScript from old browsers
...... Bunch of JavaScript Statements .......
//scriptend-->
</SCRIPT>
Note depending on your needs, JavaScript can be in Header or Body section of document

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 5 Text attributes

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. *
Secs 142.5 Full HTML Index
See Original Foil

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 6 Background color(the only HTML color that may be changed on-the-fly is the background color)

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. *
Secs 396 Full HTML Index
See Original Foil

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 7 Stanford Background Color Selector

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. *
Secs 113.7 Full HTML Index
See Original Foil

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 8 Example of Clicking on a Form - I

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 145.4 Addon Full HTML Index
<HTML><HEAD><TITLE>Javascript with Forms</TITLE>
<SCRIPT LANGUAGE="JavaScript" >
<!-- A Conventional comment to hide JavaScript from old browsers
function compute(form) {
if( confirm("Is this what you want?"))
form.result.value = eval(form.expr.value);
else alert("Enter a new expression then!"); }
//scriptend-->
</SCRIPT></HEAD>
<BODY><FORM>
Enter An Expression:
<INPUT TYPE="text" NAME="expr" SIZE=15>
<INPUT TYPE="button" VALUE="DoIt!" ONCLICK="compute(this.form)">
<BR>Result:
<INPUT TYPE="text" NAME="result" SIZE=15>
<BR>
</FORM></BODY></HTML>

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 9 Example of Clicking on a Form - II

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 25.9 Full HTML Index
Enter An Expression: 9+5
Result: 14
confirm is a native Javascript method popping up a window, requesting confirmation of requested action
alert is a native Javascript method popping up a window with a message requiring user to place OK to get rid of.
onclick="Javascript Statement Block" naturally executes statement(s) when button clicked

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 10 Example of Clicking on a Form - I

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 152.6 Addon Full HTML Index
<HTML><HEAD><TITLE>Javascript with Forms</TITLE>
<SCRIPT LANGUAGE="JavaScript" >
<!-- A Conventional comment to hide JavaScript from old browsers
function compute(form) {
if( confirm("Is this what you want?"))
form.result.value = eval(form.expr.value);
else alert("Enter a new expression then!"); }
//scriptend-->
</SCRIPT></HEAD>
<BODY><FORM>
Enter An Expression:
<INPUT TYPE="text" NAME="expr" SIZE=15>
<INPUT TYPE="button" VALUE="DoIt!" ONCLICK="compute(this.form)">
<BR>Result:
<INPUT TYPE="text" NAME="result" SIZE=15>
<BR>
</FORM></BODY></HTML>

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 11 alertmethod (this method is useful for debugging)

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. *
Secs 234.7 Full HTML Index
See Original Foil

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 12 onunload event handler

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. *
Secs 177.1 Full HTML Index
See Original Foil

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 13 Example of Parameterized HTML

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 177.1 Addon Full HTML Index
<HTML><HEAD><TITLE>Javascript for Parameterizing HTML</TITLE>
<SCRIPT LANGUAGE="JavaScript" >
<!-- A Conventional comment to hide JavaScript from old browsers
var imagewidth=600; // These could be changed by form input or some
var imagefile="npac.gif"; // computation based on size of window etc.
//scriptend-->
</SCRIPT></HEAD>
<BODY> ......Bunch of Normal Stuff
<SCRIPT LANGUAGE="JavaScript" >
<!-- A Conventional comment to hide JavaScript from old browsers
document.writeln('<img align=top width=' + imagewidth + ' src="' + imagefile + '" >');
//scriptend-->
</SCRIPT>
.... Yet More Normal Stuff
</BODY></HTML>
Note single quotes used for JavaScript, Double quotes for HTML -- can use \' if necessary to hide special meaning from JavaScript

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 14 Embedding Images in the Document

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. *
Secs 46 Full HTML Index
See Original Foil

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 15 Example of Parameterized HTML

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 87.8 Addon Full HTML Index
<HTML><HEAD><TITLE>Javascript for Parameterizing HTML</TITLE>
<SCRIPT LANGUAGE="JavaScript" >
<!-- A Conventional comment to hide JavaScript from old browsers
var imagewidth=600; // These could be changed by form input or some
var imagefile="npac.gif"; // computation based on size of window etc.
//scriptend-->
</SCRIPT></HEAD>
<BODY> ......Bunch of Normal Stuff
<SCRIPT LANGUAGE="JavaScript" >
<!-- A Conventional comment to hide JavaScript from old browsers
document.writeln('<img align=top width=' + imagewidth + ' src="' + imagefile + '" >');
//scriptend-->
</SCRIPT>
.... Yet More Normal Stuff
</BODY></HTML>
Note single quotes used for JavaScript, Double quotes for HTML -- can use \' if necessary to hide special meaning from JavaScript

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 16 LiveWire -- Netscape's Approach to CGI

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 184.3 Full HTML Index
Netscape Servers now support the "LiveWire" Extension which allows you to write "CGI Programs" in JavaScript

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 17 Refinements in the <SCRIPT> Tag - I

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 167 Full HTML Index
One can specify the different versions of the Language
  • e.g.:
  • <SCRIPT LANGUAGE="JavaScript1.1"> Ignored by Netscape Navigator 2.0
  • location.replace("versionofpagefornav3.0.html");
  • </SCRIPT>
  • <SCRIPT LANGUAGE="JavaScript1.0" > Read by Netscape Navigator 2.0
  • ..........
location.replace(URL) replaces current page with new URL which in this case is assumed to hold a version with nifty new JavaScript 1.1 features

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 18 Refinements in the <SCRIPT> Tag - II

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 100.8 Full HTML Index
This foilset assumes JavaScript 1.1 except in few cases such as Arrays where we also describe older 1.0 syntax.
<NOSCRIPT> .... </NOSCRIPT> Specifies what to do if user has disabled JavaScript in Options Menu (due to security perhaps!)
One can read JavaScript from a file
<SCRIPT SRC="JSLibrary.js">
document.write("Included File JSLibrary.js not found"); // The Navigator executes this stuff ONLY if JSLibrary.js is NOT found and so do not include useful stuff here!
</SCRIPT>
The included file must have .js suffix and server must be set up with correct MIME types to download .js files

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 19 JavaScript URL's

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 18.7 Addon Full HTML Index
One can demonstrate JavaScript as an interpreter by typing JavaScript: into location text bar of your browser.
  • This gives a two frame window where you can directly type JavaScript and see response
One can use a similar capability in HREF and other atrribute definitions
  • <a href="javascript:history.go(-1)">Go Back One Step</a>
  • This loads the last page visited in being clicked
  • Using history.go(0) would reload current page
The JavaScript Manual describes some interesting Image Map Examples including:
  • <MAP name="buttonbar">
  • <AREA shape="rect" coords="0,0,16,14"
  • HREF="javascript:top.dosomething();
  • top.frames[1].location='URLtobeloadedintoanotherframe.html';" >
  • </MAP>

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 20 Interpreted JavaScript

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. *
Secs 74.8 Full HTML Index
See Original Foil

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 21 JavaScript URL's

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 313.9 Addon Full HTML Index
One can demonstrate JavaScript as an interpreter by typing JavaScript: into location text bar of your browser.
  • This gives a two frame window where you can directly type JavaScript and see response
One can use a similar capability in HREF and other atrribute definitions
  • <a href="javascript:history.go(-1)">Go Back One Step</a>
  • This loads the last page visited in being clicked
  • Using history.go(0) would reload current page
The JavaScript Manual describes some interesting Image Map Examples including:
  • <MAP name="buttonbar">
  • <AREA shape="rect" coords="0,0,16,14"
  • HREF="javascript:top.dosomething();
  • top.frames[1].location='URLtobeloadedintoanotherframe.html';" >
  • </MAP>

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 22 JavaScript Entities in HTML

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Critical Information in IMAGE
Secs 273.6 Addon Full HTML Index
One can parameterize HTML using document.write(ln) commands such as:
document.writeln("string1" + JSvariable +"string2"); // etc.
A "lighter weight way of achieving this can be illustrated by direct HTML syntax with NO <SCRIPT> tag and no document.write
<IMG WIDTH="&{JSvar1};" SRC="&{JSvar2};" >
where JSvar1 and JSvar2 are JavaScript variables holding width and URL of image
Syntax is modeled on that for for special characters
  • &GT; is > etc.
Such JavaScript Entities can ONLY appear on RIGHT HAND side of attribute values
  • Cannot appear in HTML text such as titles.
  • document.writeln approach has no such restriction!

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 23 JavaScript Entity

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. *
Secs 118 Full HTML Index
See Original Foil

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 24 JavaScript Compared to Java - I

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * See also color IMAGE
Secs 180 Full HTML Index
JavaScript: Interpreted and not compiled by Client or Server
Java: Compiled bytecodes downloaded from server and interpreted or (Just in Time) compiled by client
JavaScript: Object based but no classes or inheritance
Java: Object-Oriented. Applets consist of object classes with inheritance
JavaScript: Code typically integrated with or embedded(Entities) in HTML (SRC="File.js" is exception)
Java: Applets distinct from HTML and accessed by Applet Tag from HTML page

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 25 JavaScript Compared to Java - II

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * See also color IMAGE
Secs 237.6 Full HTML Index
JavaScript: User has full access to capabilities of Netscape Navigator in functionality of window components
Java: User relies on Java AWT (or potentially HotJava browser) for features of Window Components
JavaScript: No explicit data types (just var) --- loose typing
Java: Variable data types must be declared and carefully "cast" to convert -- strong typing
JavaScript: Dynamic Binding. Object references are interpreted at runtime (typical interpreter)
Java: Static Binding. Object references must exist at compile time.
Security: Both JavaScript and Java applets as implemented today have significant security restrictions. This is a (Netscape) Runtime and not a language feature!

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 26 The JavaScript Language -- Values, Names Etc.

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. * Important Information in IMAGE
Secs 303.8 Addon Full HTML Index
JavaScript only has one simple type -- namely:
var anumber = 137; // or the SAME type var
var astring = "1"; // to define a string
The loose typing in JavaScript, allows interpreter to creatively interpret an expression and default (this is change from early documentation which claimed type taken from leftmost variable) is that strings have precedence over numbers so that for instance:
x = astring + anumber; // results in x = "1137"
use parseInt and parseFloat to extract numerical values from strings (see later)
Note special value null (no quotes) can be used as a null value
Strings can be delimited by '..text..' or "..text.." which are identical in meaning (unlike PERL) and one can use \n for newline and \t for TAB
Boolean literals are either true or false
Comments are /* any stuff including newlines */ or
Javascript statements; // This is a comment until new line

HELP! * YELLOW=global GREY=local HTML version of GLOBAL Foils prepared 21 February 97

Foil 27 simple addition

From Feb 12 Delivered Lecture for Course CPS616 -- Basic JavaScript Functionalities and Examples CPS616 spring 1997 -- Feb 12 1997. *
Secs 97.9 Full HTML Index
See Original Foil

Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Fri Feb 21 1997