Full HTML for

Basic foilset Overview of Advanced JavaScript -- Web Scripting Language

Given by Tom Scavo,Geoffrey C. Fox at CPS616 Technologies of the Information Age on Spring Semester 98. Foils prepared 16 June 98
Outside Index Summary of Material


Java vs. JavaScript
JavaScript Basics and Syntax
JavaScript Core Objects
JavaScript Events
Client-side JavaScript
JavaScript Object Model
Advanced Topics: Cookies, Security, LiveConnect, and LiveWire

Table of Contents for full HTML of Overview of Advanced JavaScript -- Web Scripting Language

Denote Foils where Image Critical
Denote Foils where Image has important information
Denote Foils where HTML is sufficient
denotes presence of Additional linked information which is greyed out if missing

1 JavaScript A Web Scripting Language
2 Outline
3 JavaScript Resources for Foil 3 General Remarks
4 Version History
5 Whats New in JavaScript 1.2 JavaScript 1.2
6 JScript 3.1
7 JavaScript Standards for Foil 7 ECMAScript
8 Java vs. JavaScript
9 PPT Slide
10 PPT Slide
11 Performance Issues
12 JavaScript Basics
13 JavaScript Scripts for Foil 13 JavaScript Scripts
14 "Hello World" Example
15 Simple scripts for Foil 15 "Hello World" Example (cont'd)
16 Form Example
17 Form Example Form Example (cont'd)
18 The <SCRIPT> Tag
19 The SCRIPT Tag for Foil 19 The <SCRIPT> Tag (cont'd)
20 The SRC Attribute The <SCRIPT> Tag (cont'd)
21 JavaScript URLs for Foil 21 JavaScript URLs
22 JavaScript Syntax
23 JavaScript Syntax for Foil 23 Basic Syntax
24 JavaScript Variables for Foil 24 Variables
25 Operators for Foil 25 Operators
26 Expressions for Foil 26 Expressions
27 Reserved Words
28 Statements for Foil 28 Statements
29 IF-THEN-ELSE for Foil 29 The if Statement
30 The switch Statement The switch Statement
31 Iteration and Recursion for Foil 31 The for Statement
32 General Iteration for Foil 32 The while Statement
33 More Iteration for Foil 33 The do...while Statement
34 The with Statement
35 JavaScript Core Objects
36 JavaScript Object Model for Foil 36 Basic Concepts
37 Built-in Functions for Foil 37 PPT Slide
38 The eval() Function The eval Function
39 Parsing Functions for Foil 39 Parsing Functions
40 Other Global Functions
41 Authoring Guide, Predefined Core Objects and Functions Built-in Objects
42 Array Objects for Foil 42 Array Objects
43 Array Methods Array Methods
44 Boolean Objects for Foil 44 Boolean Objects
45 The Date Object for Foil 45 Date Objects
46 Date Methods for Foil 46 Date Methods
47 Function Objects for Foil 47 Function Objects
48 Function Arguments
49 The Math Object
50 Math Properties Math Properties
51 Math Methods for Foil 51 Math Methods
52 Number Objects
53 Number Properties
54 Number Properties (cont'd)
55 Object Objects
56 RegExp Objects
57 RegExp Object RegExp Objects (cont'd)
58 Regular Expressions for Foil 58 RegExp Objects (cont'd)
59 String Objects
60 String Methods for Foil 60 String Methods
61 String Methods (cont'd)
62 JavaScript Events
63 Authoring Guide, Handling Events Events
64 Event Handlers for Foil 64 Event Handlers
65 Event Handlers for Foil 65 Event Handlers (cont'd)
66 New Event Handlers for Foil 66 Event Handlers (cont'd)
67 Event Handler Examples
68 The Event Object Event Object
69 The Event Object for Foil 69 Event Object (cont'd)
70 Client-side JavaScript
71 Authoring Guide, Using Navigator Objects Object Hierarchy
72 Object Referencing Object Referencing
73 Authoring Guide, Using Windows and Frames Window and Frame Objects
74 Window Properties for Foil 74 Window Properties
75 Window Properties (cont'd)
76 Frame Properties for Foil 76 Frame Properties
77 Frame Example (cont'd)
78 Frame Example (cont'd)
79 Frame Example (cont'd)
80 Frameset Documents for Foil 80 Frameset Documents
81 Window and Frame Methods for Foil 81 Window and Frame Methods
82 History Properties and Methods for Foil 82 History Object
83 The Location Object for Foil 83 Location Object
84 Document Properties for Foil 84 Document Object
85 Document Properties
86 Document Properties Document Properties (cont'd)
87 Document Methods for Foil 87 Document Methods
88 Form Objects for Foil 88 Form Objects
89 The form Property Form Properties
90 Form Methods
91 Image Objects Image Objects
92 Animation for Foil 92 Image Animation
93 Layers for Foil 93 Layer Objects
94 Built-in Arrays
95 HTML-reflected Arrays
96 Client-side String Methods for Foil 96 Client-side String Methods
97 Navigator Object for Foil 97 Navigator Objects
98 Displaying All MimeType Objects MimeType Objects
99 Plugins for Foil 99 Plugin Objects
100 JavaScript Object Model
101 A User-defined Object User-defined Objects
102 User-defined Methods
103 User-defined Objects for Foil 103 Another Example
104 User-defined Methods The prototype Property
105 The prototype Property The prototype Property (cont'd)
106 Prototype-based Inheritance for Foil 106 Prototype-based Inheritance
107 Advanced Topics
108 Authoring Guide, Netscape Cookies Cookies
109 Authoring Guide, Using Cookies Cookies (cont'd)
110 Cookies Cookies (cont'd)
111 Authoring Guide, JavaScript Security JavaScript Security
112 Same Origin Policy Same Origin Policy
113 The domain Property
114 Data Tainting
115 Signed Script Policy Signed Scripts
116 Object-Signing Tools Signed Scripts (cont'd)
117 Certificates
118 JAR Files
119 Signing Software with Netscape Signing Tool 1.0 Using signtool
120 Identifying Signed Scripts Writing Signed Scripts
121 Using Expanded Privileges Writing Signed Scripts (cont'd)
122 LiveAudio and LiveConnect for Foil 122 LiveConnect
123 LiveConnect for Foil 123 LiveConnect (cont'd)
124 Making the Database Connection LiveWire
125 Writing Server-side JavaScript Applications LiveWire (cont'd)

Outside Index Summary of Material



HTML version of Basic Foils prepared 16 June 98

Foil 1 JavaScript A Web Scripting Language

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Tom Scavo and Geoffrey Fox
Northeast Parallel Architectures Center
Syracuse University
111 College Place
Syracuse, NY 13244-4100
http://www.npac.syr.edu/projects/tutorials/JavaScript/
http://www.npac.syr.edu/users/gcf/javascriptjune98/

HTML version of Basic Foils prepared 16 June 98

Foil 2 Outline

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Java vs. JavaScript
JavaScript Basics and Syntax
JavaScript Core Objects
JavaScript Events
Client-side JavaScript
JavaScript Object Model
Advanced Topics: Cookies, Security, LiveConnect, and LiveWire

HTML version of Basic Foils prepared 16 June 98

Foil 3 General Remarks

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index JavaScript Resources for Foil 3
JavaScript (originally called LiveScript) is a web scripting language for clients and servers
Client-side JavaScript facilitates rapid prototyping and is more responsive than CGI
Client-side JavaScript is particularly useful for frameset documents and HTML forms
Refs: JavaScript, The Definitive Guide by David Flanagan (2nd ed, O'Reilly, 1997); The JavaScript Bible by Danny Goodman (3rd ed, IDG, 1998)

HTML version of Basic Foils prepared 16 June 98

Foil 4 Version History

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
JavaScript 1.0 debuted in NN 2.0 (Feb 96)
JavaScript 1.1 appeared in NN 3.0
NN 4.0 (aka Communicator) supports JavaScript 1.2
MSIE 3.0 introduced JScript 2.0, a subset of JavaScript 1.1
JScript 3.x is supported in MSIE 4.0
JScript 3.0 and later are ECMA-compliant [Standard ECMA-262, June 1997]

HTML version of Basic Foils prepared 16 June 98

Foil 5 JavaScript 1.2

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Whats New in JavaScript 1.2
New features in JavaScript 1.2 (NN4.0):
  • Event object and event capturing
  • Ten new event handlers
  • Nested functions; new Number() and String() functions
  • New statements: switch and do...while
  • Many new methods and properties (especially for Window, String, and Array objects)
  • Perl4-compatible regular expressions
  • Signed script security model

HTML version of Basic Foils prepared 16 June 98

Foil 6 JScript 3.1

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
New features in JScript 3.1 (MSIE4.0):
  • Conditional execution (meta-statements)
  • New event handlers (39 in all!)
  • New statements: switch and do...while
  • Two new identity operators (=== and !==)
  • Perl4-compatible regular expressions
  • Debugger support
Adequate documentation is lacking, however

HTML version of Basic Foils prepared 16 June 98

Foil 7 ECMAScript

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index JavaScript Standards for Foil 7
ECMAScript refers to standard ECMA-262 released in June 1997
ECMAScript is essentially JavaScript with all client and server-side features removed
The emerging HTML and XML Document Object Models (DOMs) will be bound initially to ECMAScript
Currently, only JScript 3.1 is ECMA-compliant

HTML version of Basic Foils prepared 16 June 98

Foil 8 Java vs. JavaScript

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Sun's Java is fast becoming a broad industry standard
Java is well designed and documented
Java is object-oriented with (single) inheritance
Java is class-based
JavaScript is primarily supported by Netscape and Microsoft
JavaScript has neither of these qualities
JavaScript is object-based with no class structure
JavaScript is prototype-based

HTML version of Basic Foils prepared 16 June 98

Foil 9 PPT Slide

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Java classes and instances are distinct
A Java class has zero or more constructors
Java property inheritance follows class hierarchy
In Java, no way to add properties at run-time
All JavaScript objects are instances
JS object definition and constructor are identical
JavaScript property inheritance follows prototype chain
JavaScript properties may be added or removed at run-time
Java vs. JavaScript (cont'd)

HTML version of Basic Foils prepared 16 June 98

Foil 10 PPT Slide

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Java applets are distinct from HTML
Java is strongly typed with static (compile-time) binding
Java bytecodes are interpreted (or "Just-In-Time" compiled) on the client
JavaScript is tightly coupled with HTML
JavaScript is loosely typed with dynamic (run-time) binding
High-level JavaScript source code is interpreted on the client
Java vs. JavaScript (cont'd)

HTML version of Basic Foils prepared 16 June 98

Foil 11 Performance Issues

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Typically, up to 106 C instructions may be executed in a few milliseconds
A Java interpeter is roughly 50 times slower than C
Java "Just-In-Time" compiler is roughly 2–8 times slower than C
Perl is 500 times slower than C
JavaScript is 5000 times slower than C

HTML version of Basic Foils prepared 16 June 98

Foil 12 JavaScript Basics

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index

HTML version of Basic Foils prepared 16 June 98

Foil 13 JavaScript Scripts

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index JavaScript Scripts for Foil 13
A script is enclosed in a <SCRIPT> container: <SCRIPT LANGUAGE="JavaScript"> ... </SCRIPT>
Scripts may be put in the <BODY> or the <HEAD>, depending on your needs
Use <NOSCRIPT>...</NOSCRIPT> for JavaScript-disabled browsers

HTML version of Basic Foils prepared 16 June 98

Foil 14 "Hello World" Example

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
<HTML>
<HEAD><TITLE>An Example</TITLE></HEAD>
<BODY>
<!-- Insert HTML here -->
<SCRIPT LANGUAGE="JavaScript">
document.writeln("<H1>Hello World!</H1>");
</SCRIPT>
<!-- Insert more HTML here -->
</BODY>
</HTML>

HTML version of Basic Foils prepared 16 June 98

Foil 15 "Hello World" Example (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Simple scripts for Foil 15
There is only one JavaScript statement: document.writeln("<H1>Hello World!</H1>");
The writeln method writes its argument into the current document
The script is in the <BODY> since it writes HTML code into the document; other scripts are written in the <HEAD>
Scripts may be hidden from old browsers

HTML version of Basic Foils prepared 16 June 98

Foil 16 Form Example

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
<HTML>
<HEAD><TITLE>JavaScript with Forms</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function compute() {
if ( window.confirm("Is this what you want?") )
aForm.result.value = eval(aForm.expr.value);
}
</SCRIPT></HEAD>
<BODY><FORM NAME="aForm"> Enter expression:
<INPUT TYPE="text" NAME="expr" SIZE=15>
<INPUT TYPE="button" VALUE="Compute!"
onClick="compute()"> <BR> Result:
<INPUT TYPE="text" NAME="result" SIZE=15>
</FORM></BODY>
</HTML>

HTML version of Basic Foils prepared 16 June 98

Foil 17 Form Example (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Form Example
confirm is a method of the Window object
eval is a built-in JavaScript function
onClick is a JavaScript event handler
Note the user-defined names (aForm, expr, result) referred to in the script

HTML version of Basic Foils prepared 16 June 98

Foil 18 The <SCRIPT> Tag

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
If you have two versions of a JavaScript script, you can load one or the other:
<SCRIPT LANGUAGE="JavaScript1.1">
// Ignored by Navigator 2.0:
location.replace("newVersion.html");
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
// Read by Navigator 2.0 and above
</SCRIPT>
The newest version is "JavaScript1.2"

HTML version of Basic Foils prepared 16 June 98

Foil 19 The <SCRIPT> Tag (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index The SCRIPT Tag for Foil 19
Multiple <SCRIPT> tags are allowed
Within a single <SCRIPT>, a function may be called before it is defined
Within a single document, a function defined in one <SCRIPT> may not be called from a previous <SCRIPT>
In particular, a function defined in the <BODY> may not be called from the <HEAD>

HTML version of Basic Foils prepared 16 June 98

Foil 20 The <SCRIPT> Tag (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index The SRC Attribute
JavaScript code may be stored in an external file (and is therefore reusable):
<SCRIPT SRC="JScode.js">
// The body of this <SCRIPT> is
// executed only if "JScode.js"
// is not found!
document.write("File not found!");
</SCRIPT>
An external file must have a .js extension and the server must be configured properly

HTML version of Basic Foils prepared 16 June 98

Foil 21 JavaScript URLs

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index JavaScript URLs for Foil 21
A trivial example of a JavaScript URL is javascript:alert("Hello World!")
A JavaScript URL may appear anywhere an ordinary URL is expected: <A HREF="javascript:history.back()">Previous Page</A>
Navigator even has a mini-scripting environment invoked by typing javascript: into the browser's location text field

HTML version of Basic Foils prepared 16 June 98

Foil 22 JavaScript Syntax

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index

HTML version of Basic Foils prepared 16 June 98

Foil 23 Basic Syntax

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index JavaScript Syntax for Foil 23
JavaScript syntax resembles Java and C
Braces { } are used for grouping
Use single or double quotes (equivalently) around string literals
Escapes: \b \t \n \f \r \" \' \\
Other literals: null, true, and false
JavaScript is case-sensitive while HTML is not (which sometimes leads to problems!)

HTML version of Basic Foils prepared 16 June 98

Foil 24 Variables

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index JavaScript Variables for Foil 24
Variables (identifiers) must begin with a letter, underscore, or dollar sign:
  • var := [a-zA-Z_$][a-zA-Z0-9_$]*
Declare variables in var statements: var aNumber = 137; var aString = "1";
JavaScript "entities" in HTML attributes: <IMG SRC="&{v1};" WIDTH="&{v2};"> where v1 and v2 are JavaScript variables

HTML version of Basic Foils prepared 16 June 98

Foil 25 Operators

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Operators for Foil 25
Assignment operators: = += -= *= /= %= <<= >>= >>>= &= ^= |=
JavaScript supports the usual arithmetic operators, as well as: % ++ --
Concatenation operator: +
Bit operators: & | ^ ~ << >> >>>
Relational operators: == > >= < <= !=
Logical operators: && || !

HTML version of Basic Foils prepared 16 June 98

Foil 26 Expressions

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Expressions for Foil 26
Examples:
  • sum += x; // sum = sum + x
  • r -= n; q++;
  • s = "temp" + 1; // assign "temp1" to s
  • phi = (1 + Math.sqrt(5))/2;
  • valid = (age > 21 && age <= 65);
An if-expression returns a value:
  • p = ( k < 0 ) ? 1/y : y;

HTML version of Basic Foils prepared 16 June 98

Foil 27 Reserved Words

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index

HTML version of Basic Foils prepared 16 June 98

Foil 28 Statements

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Statements for Foil 28
JavaScript statements include: break, continue, do, for, function, if, return, switch, var, while, and with
Multiple statements on one line are separated by semicolons
Statement blocks are delimited by braces
Comments come in two flavors: /* any text including newlines */ // comment terminated by newline

HTML version of Basic Foils prepared 16 June 98

Foil 29 The if Statement

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index IF-THEN-ELSE for Foil 29
Assuming boolean variable isWhite:
if ( isWhite ) {
document.bgColor = "pink";
isWhite = false;
} else {
document.bgColor = "white";
isWhite = true;
}
The else block is optional, of course

HTML version of Basic Foils prepared 16 June 98

Foil 30 The switch Statement

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index The switch Statement
The switch statement is new is JS 1.2: switch ( fruit ) { case "oranges": price = 0.59; break; case "apples": price = 0.32; break; case "bananas": price = 0.48; break; default: price = null; }

HTML version of Basic Foils prepared 16 June 98

Foil 31 The for Statement

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Iteration and Recursion for Foil 31
A simple for-loop:
// Compute x^k for k > 0:
for (var y=1, i=0; i<k; i++) {
y *= x;
}
Here is another type of for-loop:
for (var prop in object) {
statements
}

HTML version of Basic Foils prepared 16 June 98

Foil 32 The while Statement

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index General Iteration for Foil 32
A more general looping structure:
// Compute r = m % n for m,n > 0:
var r = m;
while ( r >= n ) {
r -= n;
}
break, labeled break, and continue are permitted in for and while loops
JavaScript supports recursion as well

HTML version of Basic Foils prepared 16 June 98

Foil 33 The do...while Statement

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index More Iteration for Foil 33
A do...while loop tests its condition at the bottom of the loop: do { statements } while ( condition );
In this case, the statements in the loop body are guaranteed to execute at least once
This loop is new in JavaScript 1.2 and JScript 3.0

HTML version of Basic Foils prepared 16 June 98

Foil 34 The with Statement

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
with ( someObject ) { statements }
someObject is the default object used for any unqualified object references: with ( Math ) { area = PI*r*r; // Math property PI x = r*cos(theta); // Math method cos y = r*sin(theta); // Math method sin }

HTML version of Basic Foils prepared 16 June 98

Foil 35 JavaScript Core Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index

HTML version of Basic Foils prepared 16 June 98

Foil 36 Basic Concepts

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index JavaScript Object Model for Foil 36
JavaScript is object-based with no classes and, hence, no (class) inheritance
An object possesses properties and methods
A property is an attribute of an object
A method acts on an object
All JavaScript objects have toString() and valueOf() methods

HTML version of Basic Foils prepared 16 June 98

Foil 37 PPT Slide

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Built-in Functions for Foil 37
The Global Object
The implicitly defined Global object is the top-level JavaScript object
It has two properties: NaN (Not-a-Number) and Infinity (but NN supports neither)
There are seven global functions:
plus the eval(...) function!

HTML version of Basic Foils prepared 16 June 98

Foil 38 The eval Function

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index The eval() Function
The function eval( string ) invokes the JavaScript interpreter on its string argument, which is evaluated in the context of the current window (i.e., globally)
  • In NN3.0 only, we may also write: object.eval( string ) In this case, the string argument is evaluated in the context of some object
  • In NN4.0, the above is identical to: with (object) eval( string );

HTML version of Basic Foils prepared 16 June 98

Foil 39 Parsing Functions

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Parsing Functions for Foil 39
parseInt(...) and parseFloat(...) convert their arguments to numbers: parseFloat("12.34"); //returns 12.34 parseInt("12.34"); //returns 12 parseFloat("12.34 ft"); //returns 12.34 parseInt("111", 2); //returns 7 parseFloat("$12.34"); //returns NaN
A handy trick: var numeric_var = string_val - 0;

HTML version of Basic Foils prepared 16 June 98

Foil 40 Other Global Functions

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
isNaN( number ) returns true if and only if its argument is NaN
isFinite( number ) returns false if its argument is NaN, Infinity, or ?Infinity; otherwise, it returns true
escape( string ) returns a URL-encoded string; unescape( string ) performs the inverse operation

HTML version of Basic Foils prepared 16 June 98

Foil 41 Built-in Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Authoring Guide, Predefined Core Objects and Functions
JavaScript has ten built-in objects:
plus the Global object discussed earlier
An instance object is created with new:
  • var today = new Date();
  • var answers = new Array(0,1,0);

HTML version of Basic Foils prepared 16 June 98

Foil 42 Array Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Array Objects for Foil 42
In NN 3.0 (and even in 2.0!), we write: var states = new Array(50); states[0] = "Alabama"; states[1] = "Alaska"; // etc.
Every array has a length property: var n = states.length; // n = 2
Two-dimensional arrays are allowed: var acodes = new Array(50); acodes[0] = new Array(205,334);

HTML version of Basic Foils prepared 16 June 98

Foil 43 Array Methods

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Array Methods
JavaScript 1.1 (and above) supports some powerful array methods:
  • myArray.sort( compFunc ) sorts myArray according to compFunc (a user-defined, numeric-valued function of two arguments) function compFunc( a, b ) { return a - b; }
  • myArray.join( sep ) returns a string of elements separated by sep
  • myArray.reverse() reverses the order of the elements in myArray

HTML version of Basic Foils prepared 16 June 98

Foil 44 Boolean Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Boolean Objects for Foil 44
To create a false Boolean object, use: new Boolean() new Boolean( 0 ) new Boolean( null ) new Boolean( "" ) new Boolean( false ) or simply use the literal false
All other values create a Boolean object with initial value true!

HTML version of Basic Foils prepared 16 June 98

Foil 45 Date Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index The Date Object for Foil 45
The Date constructor has many forms: today = new Date(); xmas = new Date( 97, 12, 25 ); birthday = new Date( "April 6, 1952" );
  • The Date() constructor may also be used as a function with no arguments
  • Time measured in milliseconds past January 1, 1970 UTC (Universal Coordinated Time)
  • Warning: The Date object is buggy in NN2!

HTML version of Basic Foils prepared 16 June 98

Foil 46 Date Methods

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Date Methods for Foil 46
The Date object has no properties; access to date and time fields is via dozens of "set" and "get" methods: xmas.setYear(xmas.getYear()+1);
There are two "static" Date methods:
  • Date.parse( date_string )
  • Date.UTC( year, month, day )
Both convert their argument(s) to milliseconds past January 1, 1970 UTC

HTML version of Basic Foils prepared 16 June 98

Foil 47 Function Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Function Objects for Foil 47
Every function statement gives rise to a Function object
Function objects are also created with the new operator: window.onload = new Function( "document.bgColor='white'" );
Function objects may be anonymous
NB: Function arguments are passed by value

HTML version of Basic Foils prepared 16 June 98

Foil 48 Function Arguments

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Functions may take an arbitrary number of arguments: function User() { this.name = User.arguments[0]; this.group = User.arguments[1]; this.email = new Array(); var n = User.arguments.length; for (var i = 2; i < n; i++) this.email[i-2]=User.arguments[i]; }
Note: the arguments[] array is built in

HTML version of Basic Foils prepared 16 June 98

Foil 49 The Math Object

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Math is the only built-in object that does not have a constructor
Its sole purpose is to provide constants (properties) and functions (methods)
The with statement is particularly useful in conjunction with Math: with ( Math ) { x = r*cos( PI/2 ); y = r*sin( PI/2 ); }

HTML version of Basic Foils prepared 16 June 98

Foil 50 Math Properties

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Math Properties
These properties are primarily for convenience:

HTML version of Basic Foils prepared 16 June 98

Foil 51 Math Methods

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Math Methods for Foil 51
Each method in the middle column requires two arguments

HTML version of Basic Foils prepared 16 June 98

Foil 52 Number Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
There is a Number constructor, but it is rarely used since JavaScript automatically converts numeric literals to numeric objects as needed
Some useful properties are provided: MAX_VALUE, MIN_VALUE, NaN, NEGATIVE_INFINITY, and POSITIVE_INFINITY

HTML version of Basic Foils prepared 16 June 98

Foil 53 Number Properties

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Number.MIN_VALUE is the smallest positive number representable
Number.POSITIVE_INFINITY is returned by any operation that is greater than Number.MAX_VALUE
Number.NEGATIVE_INFINITY is returned by any operation that is less than ?Number.MAX_VALUE

HTML version of Basic Foils prepared 16 June 98

Foil 54 Number Properties (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Number.NaN is returned by parseInt(...) and parseFloat(...) when the parsing operation fails
An attempt to divide by zero also returns Number.NaN
Test for Number.NaN using the global boolean function isNaN()
Note: NN2.0 and MSIE3.0 do not support Number.NaN!

HTML version of Basic Foils prepared 16 June 98

Foil 55 Object Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
The Object constructor converts its argument (any numeric, boolean, or string literal) to a JavaScript object: var s = new Object( "NPAC" ); is equivalent to var s = new String( "NPAC" ); This is usually unnecessary, however
The corresponding function is Object(...)

HTML version of Basic Foils prepared 16 June 98

Foil 56 RegExp Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
An RegExp object (new in JavaScript 1.2) corresponds to a regular expression
There are two syntaxes: regexp1 = new RegExp( "pattern" ); regexp2 = /pattern/;
The latter "compiles" the pattern: regexp2 == regexp1.compile("pattern")
The RegExp object has two other methods: exec( str ) and test( str )

HTML version of Basic Foils prepared 16 June 98

Foil 57 RegExp Objects (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index RegExp Object
Here are two examples: function isDigit( str ) { var regexp = /^[0-9]*$/; return regexp.test( str ); } function isSSN( str ) { // match 999999999 or 999-99-9999 var regexp = /^(\d{9}|\d{3}-\d{2}-\d{4})$/; return regexp.test( str ); }

HTML version of Basic Foils prepared 16 June 98

Foil 58 RegExp Objects (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Regular Expressions for Foil 58
String methods have been added to JavaScript 1.2 to handle regular expressions: search( regexp ); // like test(...) match( regexp ); // like exec(...) replace( regexp, newsubstr );
match(...) returns an array of strings
The split(...) method now takes a regular expression as optional parameter: var strArray = str.split( /\s*;\s*/ );

HTML version of Basic Foils prepared 16 June 98

Foil 59 String Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
String objects are created as in var myStr = new String("NPAC"); or more simply with var myStr = "NPAC";
Actually, the latter is a string literal, but these are automatically converted as needed
String characters are indexed starting with 0
The String object has one property: var n = myStr.length;

HTML version of Basic Foils prepared 16 June 98

Foil 60 String Methods

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index String Methods for Foil 60
Core JavaScript String methods:
  • charAt( pos )
  • charCodeAt( pos )
  • indexOf( searchstring, pos )
  • lastIndexOf( searchstring, pos )
  • split( separator )
  • substring( start, end )
  • toLowerCase() and toUpperCase()
Static method: String.fromCharCode(...)

HTML version of Basic Foils prepared 16 June 98

Foil 61 String Methods (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Client-side JavaScript defines more than a dozen String methods (discussed later)
Six new methods added in JavaScript 1.2:
  • concat( string2 )
  • match( regexp )
  • replace( regexp, newSubstr )
  • search( regexp )
  • slice( begSlice, endSlice )
  • substr( start, length )

HTML version of Basic Foils prepared 16 June 98

Foil 62 JavaScript Events

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index

HTML version of Basic Foils prepared 16 June 98

Foil 63 Events

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Authoring Guide, Handling Events
Events are associated with
  • forms
  • hyperlinks
  • images and image maps
  • loading and unloading of documents
  • input focus of a window or form element
JavaScript introduces event handlers to handle these events

HTML version of Basic Foils prepared 16 June 98

Foil 64 Event Handlers

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Event Handlers for Foil 64

HTML version of Basic Foils prepared 16 June 98

Foil 65 Event Handlers (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Event Handlers for Foil 65

HTML version of Basic Foils prepared 16 June 98

Foil 66 Event Handlers (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index New Event Handlers for Foil 66
JavaScript 1.2 adds ten new event handlers
Three existing handlers (onClick, onMouseOut, and onMouseOver) have new properties
All these events are supported by IE4.0 except onDragDrop

HTML version of Basic Foils prepared 16 June 98

Foil 67 Event Handler Examples

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
The onLoad event handler is triggered when a page is loaded or reloaded : <BODY onLoad="init()"> Similarly, the onUnload handler is called when a page is exited
Here's another example:
<FORM
onSubmit="return check(this)">
...
</FORM>

HTML version of Basic Foils prepared 16 June 98

Foil 68 Event Object

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index The Event Object
An Event object (new in JavaScript 1.2) is passed to an event handler when an event occurs
There are 23 types of events, one for each handler: for example, a Click event is passed to the onClick handler when a link or form element is clicked
An Event object has up to 11 properties

HTML version of Basic Foils prepared 16 June 98

Foil 69 Event Object (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index The Event Object for Foil 69
Properties of an Event object:
The Click object, for example, supports all but the data property (note: the *X and *Y properties are undefined for button clicks)

HTML version of Basic Foils prepared 16 June 98

Foil 70 Client-side JavaScript

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index

HTML version of Basic Foils prepared 16 June 98

Foil 71 Object Hierarchy

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Authoring Guide, Using Navigator Objects

HTML version of Basic Foils prepared 16 June 98

Foil 72 Object Referencing

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Object Referencing
A fully-qualified reference: window.document.myForm.myButton.value
window (often suppressed) and document are pre-defined objects
myForm and myButton are user-defined object names
value is a property of the Button object (and most other form elements)

HTML version of Basic Foils prepared 16 June 98

Foil 73 Window and Frame Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Authoring Guide, Using Windows and Frames
A Window object--the top-level JavaScript object--is the reflection of a <BODY> or <FRAMESET> tag
A browser window may be divided into subwindows called frames
A Frame object--the reflection of a <FRAME> tag--"inherits" all of its properties and methods from the Window object

HTML version of Basic Foils prepared 16 June 98

Foil 74 Window Properties

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Window Properties for Foil 74
window and self are synonyms for the current window
top refers to the highest-level window in a hierarchy of frames
parent refers to the previous window or frame in a hierarchy of frames
A window may have a name: myWin = window.open(URL);
Note: The opener property is not supported in NN2.

HTML version of Basic Foils prepared 16 June 98

Foil 75 Window Properties (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
An analogy with the UNIX shell:

HTML version of Basic Foils prepared 16 June 98

Foil 76 Frame Properties

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Frame Properties for Foil 76
A Frame object also has properties called window, self, and parent
The most important property of the Frame object is the frames[] array
Each <FRAME> tag automatically creates an entry in the frames[] array
Frames may also have names, as in <FRAME NAME="upperFrame">

HTML version of Basic Foils prepared 16 June 98

Foil 77 Frame Example (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
<!-- File: index.html -->
<HTML>
<FRAMESET ROWS="90%,10%">
<FRAME SRC="skeleton.html"
NAME="upperFrame">
<FRAME SRC="navigate.html"
NAME="navigateFrame">
</FRAMESET>
<NOFRAMES>...</NOFRAMES>
</HTML>

HTML version of Basic Foils prepared 16 June 98

Foil 78 Frame Example (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
<!-- File: skeleton.html -->
<HTML>
<FRAMESET ROWS="30%,70%">
<FRAME SRC="category.html"
NAME="listFrame">
<FRAME SRC="titles.html"
NAME="contentFrame">
</FRAMESET>
</HTML>

HTML version of Basic Foils prepared 16 June 98

Foil 79 Frame Example (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Absolute references:
  • top.navigateFrame
  • top.upperFrame
  • top.upperFrame.contentFrame
  • top.upperFrame.listFrame
Relative references:
  • parent.upperFrame
  • parent.contentFrame
  • parent.parent.navigateFrame
In which documents are
these references valid?

HTML version of Basic Foils prepared 16 June 98

Foil 80 Frameset Documents

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Frameset Documents for Foil 80
The <FRAMESET> and <BODY> tags are mutually exclusive
No closing tag for <FRAME> is needed
A frameset document may contain a <SCRIPT> in its <HEAD>
Use <NOFRAMES> inside <FRAMESET> for frames-impaired browsers

HTML version of Basic Foils prepared 16 June 98

Foil 81 Window and Frame Methods

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Window and Frame Methods for Foil 81
Both Window and Frame objects have blur, focus, clearTimeout, and setTimeout methods
The Window object also has alert, close, confirm, open, and prompt methods. For example, var msg = "The alert() method\n"; msg += " is handy for debugging."; window.alert( msg );

HTML version of Basic Foils prepared 16 June 98

Foil 82 History Object

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index History Properties and Methods for Foil 82
There is one History (and Location) object per window or frame
The History object is essentially an array of URLs (called history[]), which most browsers restrict access to
Two methods back() and forward() perform the same functions as the corresponding browser buttons

HTML version of Basic Foils prepared 16 June 98

Foil 83 Location Object

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index The Location Object for Foil 83
The Location object corresponds to the URL of the current document
To load a new document into the current window, use: window.location.href = "foo.html"; or simply window.location = "foo.html";
Location object properties: href, protocol, host, pathname, search

HTML version of Basic Foils prepared 16 June 98

Foil 84 Document Object

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Document Properties for Foil 84
There is one Document object (called document) per window or frame
The Document object has numerous subobjects (Anchor, Applet, Embed, Area, Form, Image, Link, Layer) and property arrays (anchors[], applets[], embeds[], forms[], images[], links[], layers[])

HTML version of Basic Foils prepared 16 June 98

Foil 85 Document Properties

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
A Document object has the following color properties: alinkColor, linkColor, vlinkColor, bgColor, and fgColor
Other Document properties include lastModified and URL: with ( document ) { writeln('<TT>', URL, '</TT><BR>'); writeln('Updated: ', lastModified); }

HTML version of Basic Foils prepared 16 June 98

Foil 86 Document Properties (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Document Properties
To list the properties of document:
for ( var prop in document ) {
with ( document ) {
write( prop + " = " );
writeln( eval(prop), "<BR>");
}
}
Recall that the with statement qualifies all object references within its scope

HTML version of Basic Foils prepared 16 June 98

Foil 87 Document Methods

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Document Methods for Foil 87
The write(...) and writeln(...) methods take a comma-separated list of string expressions
The open() and close() methods (not to be confused with window.open() and window.close()) open and close a document for writing, but these are seldom used explicitly

HTML version of Basic Foils prepared 16 June 98

Foil 88 Form Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Form Objects for Foil 88
Every Form object is the reflection of an HTML <FORM> tag
The forms[] array may be indexed by integer or name (i.e., forms[0] or forms['myForm'] )
A Form object has many subobjects, each corresponding to an HTML form element. These are reflected in the elements[] array

HTML version of Basic Foils prepared 16 June 98

Foil 89 Form Properties

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index The form Property
Form elements are reflected in the elements[] array. For example, document.forms[0].elements[1]; is the second element of the first form
Most Form properties (action, encoding, method, and target) are read/write variables, that is, these properties may be modified on-the-fly

HTML version of Basic Foils prepared 16 June 98

Foil 90 Form Methods

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Invoking the submit() or reset() method on a Form object has the same effect as pressing the corresponding button
The event handlers onSubmit and onReset may be used to override the default submit and reset actions.

HTML version of Basic Foils prepared 16 June 98

Foil 91 Image Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Image Objects
Images may be pre-loaded (they should be cached in the <HEAD>, especially if they are to be used in the <BODY>): var images = new Array( num ); for ( var i = 0; i < num; i++ ) { images[i] = new Image(); images[i].src = "image"+ i +".gif"; }
This code loads files "image1.gif", "image2.gif", and so on

HTML version of Basic Foils prepared 16 June 98

Foil 92 Image Animation

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Animation for Foil 92
Now suppose we have the tag <IMG NAME=img> in the <BODY>. The following recursive method animates the cached images: var n = 0; function animate() { document.img.src = images[n].src; n = (n + 1) % images.length; id = setTimeout("animate()", 250); }

HTML version of Basic Foils prepared 16 June 98

Foil 93 Layer Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Layers for Foil 93
Think of a Layer object (new in JS 1.2) as a dynamic document within a document
Each <LAYER> tag generates a Layer object and a corresponding element in the layers[] array
Layers have many methods and properties (most of which may be modified on-the-fly)
Note: Layers are not supported in MSIE

HTML version of Basic Foils prepared 16 June 98

Foil 94 Built-in Arrays

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Built-in Arrays
JavaScript has numerous built-in arrays, each with its own length property:
Plus the history array--the only one whose name does not end in "s"!

HTML version of Basic Foils prepared 16 June 98

Foil 95 HTML-reflected Arrays

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Plus the elements array, whose parent is the Form object

HTML version of Basic Foils prepared 16 June 98

Foil 96 Client-side String Methods

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Client-side String Methods for Foil 96
Client-side JavaScript defines more than a dozen String methods, including:

HTML version of Basic Foils prepared 16 June 98

Foil 97 Navigator Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Navigator Object for Foil 97
The Navigator object contains information about the browser
Two properties are appName and appVersion
Methods include javaEnabled and taintEnabled
All windows share the same Navigator object, which is truly global

HTML version of Basic Foils prepared 16 June 98

Foil 98 MimeType Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Displaying All MimeType Objects
The MimeType object is a subobject of the Navigator object
The mimeTypes[] array contains an entry for each MIME type supported by the browser
Properties of MimeType include description, type, and suffixes
The property enabledPlugin refers to a Plugin object

HTML version of Basic Foils prepared 16 June 98

Foil 99 Plugin Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Plugins for Foil 99
Like MimeType, the Plugin object is a subobject of the Navigator object
The plugins[] array contains an entry for each installed plugin
Each Plugin object is an array of MimeType objects. For example, navigator.plugins[0][0].type is a MIME type supported by plugins[0]

HTML version of Basic Foils prepared 16 June 98

Foil 100 JavaScript Object Model

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index

HTML version of Basic Foils prepared 16 June 98

Foil 101 User-defined Objects

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index A User-defined Object
Objects are defined with the function statement. The following Circle object, for example, has property r: function Circle( r ) { this.r = ( r > 0 ) ? r : 0; }
The this keyword permits this function to be used as a constructor: var c = new Circle( 2.0 ); var area = Math.PI * c.r * c.r;

HTML version of Basic Foils prepared 16 June 98

Foil 102 User-defined Methods

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
User-defined Methods
Methods are defined as Function objects: function Circle( r ) { this.r = ( r > 0 ) ? r : 0; this.getRadius = new Function( "return this.r" ); this.setRadius = new Function( "r", "this.r = r" ); }
Note: The last argument of the Function constructor is implicitly the method body

HTML version of Basic Foils prepared 16 June 98

Foil 103 Another Example

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index User-defined Objects for Foil 103
Here's another example: function Car( make, model ) { this.make = make || ""; this.model = model || ""; this.color = null; this.setColor = new Function( "color", "this.color = color" ); }
Instantiate a Car object with new: myCar = new Car( "Ford", "Explorer" ); myCar.setColor( "red" );

HTML version of Basic Foils prepared 16 June 98

Foil 104 The prototype Property

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index User-defined Methods
Methods may be added after the fact: function Circle_area() { return Math.PI * this.r * this.r; } Circle.prototype.area = Circle_area;
Use the previous method as follows: var radius = 1/Math.sqrt( Math.PI ); var c = new Circle( radius ); var area = c.area();

HTML version of Basic Foils prepared 16 June 98

Foil 105 The prototype Property (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index The prototype Property
We can add methods to built-in objects, too: // Does an array contain element x ? function contains( x ) { for (var i=0; i<this.length; i++) { if (this[i] == x) return true; } return false; }
Now add the method to the Array object with the prototype property: Array.prototype.contains = contains;

HTML version of Basic Foils prepared 16 June 98

Foil 106 Prototype-based Inheritance

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Prototype-based Inheritance for Foil 106
Define the parent object: function Ellipse( r1, r2 ) { this.r1 = ( r1 > 0 ) ? r1 : 0; this.r2 = ( r2 > 0 ) ? r2 : 0; this.area = new Function( "return Math.PI * this.r1 * this.r2" ); }
Define the child object: function Circle( r ) { this.r = ( r > 0 ) ? r : 0; this.parent = Ellipse; this.parent( r, r ); } Circle.prototype = new Ellipse;

HTML version of Basic Foils prepared 16 June 98

Foil 107 Advanced Topics

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index

HTML version of Basic Foils prepared 16 June 98

Foil 108 Cookies

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Authoring Guide, Netscape Cookies
A cookie is:
  • a named string stored by the browser
  • a property of the Document object
The cookie mechanism gives the browser a kind of memory, that is, a cookie "saves state"
Originally, only CGI scripts could read/write cookie strings, but with JavaScript, cookies are handled entirely on the client side

HTML version of Basic Foils prepared 16 June 98

Foil 109 Cookies (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Authoring Guide, Using Cookies
A cookie
  • persists for the duration of the browser session (but an expiration date may be given)
  • is associated with the subtree of the document that created it (but a cookie path may be specified)
  • is accessible to pages on the server that created it (but a cookie domain may be declared)

HTML version of Basic Foils prepared 16 June 98

Foil 110 Cookies (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Cookies
A cookie string may not contain semicolons, commas, or whitespace (use escape() and unescape())
Minimum specifications:
  • No more than 300 cookies per client
  • No more than 20 cookies per server
  • No more than 4Kb per cookie

HTML version of Basic Foils prepared 16 June 98

Foil 111 JavaScript Security

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Authoring Guide, JavaScript Security
When browsing the Web, two pieces of information are considered public: the IP address of the client and the type of browser. All other data are considered private. In particular,
  • JavaScript does not permit access to the client file system
  • JavaScript can not establish a direct connection to an arbitrary Internet host

HTML version of Basic Foils prepared 16 June 98

Foil 112 Same Origin Policy

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Same Origin Policy
An important bug fix appeared in NN2:
  • Scripts from one server may not read properties of windows or documents from another server
See this URL for an interesting account: http://www.osf.org/~loverso/javascript/
For example, a script from home.netscape.com may not read the properties of a document loaded from developer.netscape.com

HTML version of Basic Foils prepared 16 June 98

Foil 113 The domain Property

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
The document.domain property is initialized to the hostname of the server from which the document was loaded
This property may be set to any valid domain suffix of itself
For example, if document.domain is "home.netscape.com", it may be set to "netscape.com" (but not "ape.com")

HTML version of Basic Foils prepared 16 June 98

Foil 114 Data Tainting

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Data tainting, an alternative to the Same Origin Policy, was experimentally implemented in NN3.0
Data tainting allows access to private data (e.g., history[] array) but forbids "export" of this data over the Internet
Both data and methods may be tainted
Tainting has been disabled in NN4, in favor of signed scripts

HTML version of Basic Foils prepared 16 June 98

Foil 115 Signed Scripts

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Signed Script Policy
A new security model called signed scripts was implemented in NN4.0
Signed scripts are based upon the Java security model for signed objects
The following objects may be signed:
  • inline scripts specified with the <SCRIPT> tag
  • separate source files given by the SRC attribute
  • event handlers
  • JavaScript entities

HTML version of Basic Foils prepared 16 June 98

Foil 116 Signed Scripts (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Object-Signing Tools
Scripts are signed using Netscape's Signing Tool (signtool), a command-line utility that creates digital signatures for Java class files, JavaScript scripts, plugins, etc.
Scripts may be served from a secure (SSL) server, in which case they're treated as if signed with the public key of that server
Users retain the right to deny the privileges requested by the signed script

HTML version of Basic Foils prepared 16 June 98

Foil 117 Certificates

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
A certificate is an electronic document used to identify an individual or organization
An object-signing certificate is a special kind of certificate that allows you to associate your digital signature with one or more files
A digital signature is analogous to a handwritten signature (that is, it is difficult to refute and may be legally binding)

HTML version of Basic Foils prepared 16 June 98

Foil 118 JAR Files

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index
Signed objects are packaged in a JAR (Java Archive) file, a sort of "digital envelope" based on the ZIP archive format
Using signtool, one associates digital signatures with the files in a JAR, which:
  • confirm the identity of the entity whose digital signature is associated with the JAR
  • check whether the files have been tampered with since being signed

HTML version of Basic Foils prepared 16 June 98

Foil 119 Using signtool

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Signing Software with Netscape Signing Tool 1.0
First check to see what object-signing certificates are available: % signtool -l
If none are available, a test certificate may be created with the command: % signtool -G MyTestCert
To sign scripts in directory "signdir", type: % signtool -k MyTestCert -J signdir

HTML version of Basic Foils prepared 16 June 98

Foil 120 Writing Signed Scripts

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Identifying Signed Scripts
First, identify inline scripts to be signed: <SCRIPT ... ARCHIVE="some.jar" ID="1">
The script ID must be unique in the JAR
JavaScript source files don't need an ID: <SCRIPT ... ARCHIVE="some.jar" SRC="file.js">
Note: Only one ARCHIVE attribute per document is necessary, in which case all signed objects are stored in a single JAR

HTML version of Basic Foils prepared 16 June 98

Foil 121 Writing Signed Scripts (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Using Expanded Privileges
Signed scripts may request expanded privileges
For example, to access private data: netscape.security.PrivilegeManager. enablePrivilege( "UniversalBrowserRead" );
"UniversalBrowserRead" is just one of many target privileges that may be requested

HTML version of Basic Foils prepared 16 June 98

Foil 122 LiveConnect

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index LiveAudio and LiveConnect for Foil 122
LiveConnect is a communications protocol for Java applets, plugins, and JavaScript
A JavaObject is a JavaScript wrapper around a Java object; a JSObject is a Java wrapper around a JavaScript object
MSIE3.0 does not support LiveConnect; instead, applets are treated as ActiveX objects, but fortunately the basic syntax is the same

HTML version of Basic Foils prepared 16 June 98

Foil 123 LiveConnect (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index LiveConnect for Foil 123
With LiveConnect:
  • JavaScript can access the classes of the browser's Java virtual machine
  • JavaScript can read/write the public variables of an applet and invoke public methods; same for Java-enabled plug-ins
  • Applets and Java-enabled plug-ins can read/write JavaScript object properties and invoke JavaScript functions

HTML version of Basic Foils prepared 16 June 98

Foil 124 LiveWire

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Making the Database Connection
LiveWire is Netscape's proprietary alternative to CGI, which lets developers create database applications in JavaScript
It consists of two components: a compiler and a server extension (both require Enterprise Server)

HTML version of Basic Foils prepared 16 June 98

Foil 125 LiveWire (cont'd)

From Overview of Advanced JavaScript -- Web Scripting Language CPS616 Technologies of the Information Age -- Spring Semester 98. *
Full HTML Index Writing Server-side JavaScript Applications
Server-side JavaScript is used to connect to and interact with a database server
A server-side object called DbPool lets you (efficiently) connect to multiple databases or multiple times to a single database
Client-side JavaScript is contained in <SCRIPT> tags; server-side JavaScript is contained in <SERVER> tags and must be pre-compiled on the server

© 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 Sun Nov 29 1998