JavaScript #4 Instructor: Roseann Krane
JavaScript is capable of doing more functions than simply writing text to the screen. The conditional statement provides the ability to make decisions. The conditional statement evaluates a condition and then takes different actions depending on the results of that evaluation.
The keywords are if, else, and return and the condition is followed by a statement block which consists of an opening brace, statements and a closing brace. Example:
if (<condition>)
{
statement 1;
statement 2;
statement N;
}
else
{
statement e1;
statement e2;
statement eN;
}
The JavaScript condition will always consist of two tokens separated by a relation operator. A token can be either a variable name or a literal constant. The relational operator may be any of the following:
Operator Meaning
== is equal to
!= is not equal to
< is less than
> is greater than
<=
is less than or equal to
>=
is greater than or equal to
Task to do: Add the following script to your page:
<SCRIPT>
if (navigator.appName ==
"Netscape")
{
document.write ("You are using Netscape Navigator.");
}
else
{
document.write ("You are not using Netscape.<BR>");
document.write ("I'll bet your're using Internet Explorer.");
}
</SCRIPT>
JavaScript alert() will allow the program to display a special dialog box that will alert the user that an expected event has occurred, or that some kind of user input is required. Try it:
Add the alert statement after the first document.write in your script:
alert ("Netscape Navigator detected.");
and add a second alert to the else block:
alert ("Netscape Navigator required.");
JavaScript Events available:
Event Name Event Trigger
onAbort
The user aborted the loading of a Web page.
onBlur
The user deactivated an object (the object lost focus).
onChange
The user changed an object in some way.
onClick
The user clicked the mouse on an object.
onError
The JavaScript interpreter encountered a script error.
onFocus
The user activated an object (the object received focus).
onLoad
The Web browser finished loading a page.
onMouseOver The mouse pointer passed over an object.
onMouseOut The mouse pointer moved off of an object.
onSelect
The user selected (highlighted) the contents of an object.
onSubmit
The user submitted a HTML form.
onUnload
The Web browser unloaded a page from memory.
Task to do:
Seach the web for examples of the events which we haven't used. Send email to the teacher's class account giving the url of two examples other than onLoad or onMouseOver.