This JavaScript Guide was last modified
Introduction
What is JavaScript?
JavaScript is a cross-platform, object-based scripting language
Netscape provides on-line JavaScript documentation
Client-side JavaScript is embedded directly in HTML pages and is interpreted by browser at runtime
Client-side JavaScript statements embedded in HTML page can respond to user events such as mouse-clicks, form input, and page navigation
Microsoft Internet Explorer also supports JavaScript, but has some additional features and calls its version "JScript"
JavaScript syntax much like Java, C, C++
JavaScript language resembles Java but does not have Java's static typing and strong type checking. That means you do not have to declare what type a variable is in JavaScript like you have to in Java.
JavaScript and Java have nearly same expression syntax and basic control-flow constructs
JavaScript -- Interpreted (not compiled) by client
Java -- Compiled bytecodes downloaded from server, executed on client
JavaScript -- Code integrated with, and embedded in, HTML
Java -- Applets distinct from HTML (but accessed from HTML pages)
JavaScript Basics
Values -- numbers (42, 3.14159), logical values (true, false), strings ("Purdue University")
Use variables as symbolic names for values. Must start with letter or underscore (_). Subsequent characters can also be digits (0-9).
Declare a variable via
var x;
Character string is zero, one, or more characters enclosed in double (") or single (') quotation marks
Expression is any valid set of values, variables, operators, and expressions that evaluates to single value; value can be number, string, or logical value
Basic assignment operator is equal (=)
Standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/)
Comparison operators are equal (==), not equal (!=), greater than (>), greater than or equal (>=), less than (<), less than or equal (<=)
JavaScript is object-based
Object has properties (fields)
Object has functions associated with it that are known as the object's methods
Function is JavaScript procedure -- set of statements that performs specific task
function
keyword
if...else Statement
");
break;
case "Apples" :
document.write("Apples are
$0.32 a pound.
");
break;
case "Bananas" :
document.write("Bananas are
$0.48 a pound.
");
break;
case "Cherries" :
document.write("Cherries are
$3.00 a pound.
");
break;
default :
document.write("Sorry, we are out of "
+ fruit + ".
");
}
What We Can Do with JavaScript
(1) Build HTML dynamically
(2) Monitor user events and take actions -- such as...
clicking button or link
changing form field
moving mouse on or off link
Embedding JavaScript in HTML
SCRIPT tag is extension to HTML that can enclose JavaScript statements
Browser executes code within SCRIPT container.
Some browsers do NOT support JavaScript. To ensure that these browsers ignore the JavaScript code, place entire script within HTML comment tags, and precede ending comment tag with double-slash (//) that indicates JavaScript single-line comment.
SRC attribute of SCRIPT tag lets you specify file as JavaScript source (rather than embedding JavaScript in HTML)
JavaScript statements within SCRIPT tag with SRC attribute are ignored unless inclusion has error
Functions
Define functions for page in HEAD portion of document
JavaScript Adding Machine that does NOT work
JavaScript Adding Machine that DOES work
That is all....
write method of document displays output on screen
Using the write method
This is some standard HTML,
unlike the above that is generated.
Using the JavaScript Console
Suppose that you make an error in your JavaScript
Type "javascript:" into Location for details
But this will not catch all errors
Using JavaScript Objects
navigator: properties for name and version of browser being used
window: properties for entire window (also window object for each window in framed document)
document: properties for document content, such as title, background color, links, forms
To refer to specific properties, specify object name and all ancestors
Generally, object gets name from NAME attribute of corresponding HTML tag
value
property of schoolname
text field in form named sform
in current
document
Window Methods
alert: displays an alert box with message
confirm: displays confirm dialog box with OK and Cancel buttons
prompt: displays prompt dialog box with text field for entering a value
setTimeout: evaluates expression or calls function once after specified period elapses (in milliseconds -- thousandths of seconds)
setInterval: evaluates expression or calls function each time specified period elapses (in milliseconds)
location: redirects client to another URL as if user had clicked hyperlink
document object properties: title, bgColor, fgColor, linkColor, alinkColor, vlinkColor, lastModified (date document was last modified), referrer (previous URL client visited), and URL (URL of document)
window object properties: innerHeight, innerWidth, outerHeight, outerWidth (inner and outer height and width of browser window -- can be changed to resize window)
navigator object properties: appName (name of browser ... Netscape), appVersion (version information ... 4.7)
");
document.writeln("URL --",
document.URL, "
");
document.writeln("Background color --",
document.bgColor, "
");
document.writeln("Last Modified --",
document.lastModified, "
");
document.writeln("Referred by --",
document.referrer, "
");
document.writeln("Inner height of window --",
window.innerHeight, "
");
document.writeln("Inner width of window --",
window.innerWidth, "
");
document.writeln("Outer height of window --",
window.outerHeight, "
");
document.writeln("Outer width of window --",
window.outerWidth, "
");
document.writeln("Web browser is --",
navigator.appName);
document.writeln(" version ",
navigator.appVersion, "
");
Handling Events
Events are actions that occur usually as result of something user
does --
clicking button or link
changing form field
moving mouse on or off link
Event handlers and events:
onFocus - user gives input focus to window or form element
onBlur - user removes input focus from window or form element
onChange - user changes text field, textarea, select list
onClick - user clicks button or link
onLoad - (used in <BODY> or <FRAMESET>) done when Web page loads initially
onMouseOver - user moves cursor over link
onMouseOut - user moves cursor out of client-side image map or link
onSubmit - user clicks SUBMIT button in form
this
refers to current object -- the button
this.form
refers to form containing the button
JavaScript onMouseOver can be used along with HREF in a standard link or in a client side image map
JavaScript onMouseOver can be used to change the appearance of images
OnMouseOver can also be used for changing images and popping up alerts
The Elusive "Click Here" Image
"Real world" example of OnMouseOver and OnMouseOut
Animated Images Using JavaScript
Protecting Images Using JavaScript
An Evolving Background Color Using JavaScript
Using the Status Bar
Two window properties display messages in the browser status bar
defaultStatus
appears when nothing else is in status bar
status
displays transient message in status bar
Validating Form Input
Download each of the following JavaScript and mailto examples, change
the "mailto" field, and try them out:
(1) Confirm information received via
Browser window
(2) Confirm information received via
JavaScript confirmation mini-window
As user enters it, onChange event handler on each form element that you want validated
When user submits form, onClick event handler on button that submits form
Validating Form Input
Using Windows and Frames
Can create a window with the open method
Closing windows and creating a back button (this one opens in a new window by using TARGET="_blank")
Closing windows and creating a back button (this one opens in the same window)
Creating a form with a selected mailto that opens a window
Creating a Frame
listFrame
is top.frames[0]
contentFrame
is top.frames[1]
navigateFrame
is top.frames[2]
Using JavaScript URLs