Lecture 6
Lecture 6
Introduction
• What is it?
• How does it work?
• What is Java?
• Learning JavaScript
• JavaScript Statements
• JavaScript and HTML forms
What is JavaScript?
• Improve appearance
• Especially graphics
• Visual feedback
• Site navigation
• Perform calculations
• Validation of input
• Other technologies
How Does It Work?
• Totally different
• A full programming language
• Much harder!
• A compiled language
• Independent of the web
• Sometimes used together
Learning JavaScript
Object JavaScript
Object Name
The browser window window
A frame within the browser window frame
History list containing the Web page history
The browser being run by the user navigator
The URL of the current web page location
Web page shown in browser window document
A form on the current Web page form
DOM (Document Object Model)
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<script language="JavaScript">
document.write('This is my first
JavaScript Page');
Note the symbol for
</script> line continuation
</body>
</html>
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<script language=“JavaScript">
document.write('<h1>This is my first
JavaScript Page</h1>');
HTML written
</script> inside JavaScript
</body>
</html>
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<p>
<a href="myfile.html">My Page</a>
<br />
<a href="myfile.html"
onMouseover="window.alert('Hello');">
My Page</A>
</p>
</body> An Event JavaScript written
</html> inside HTML
Example Statements
<script language="JavaScript">
window.prompt('Enter your name:','');
Another event
</script>
<form>
<input type="button" Value="Press"
onClick="window.alert('Hello');">
</form>
Note quotes: " and '
HTML Forms and JavaScript
<form name="addressform">
Name: <input name="yourname"><br />
Phone: <input name="phone"><br />
Email: <input name="email"><br />
</form>
Forms and JavaScript
document.formname.elementname.value
Thus:
document.addressform.yourname.value
document.addressform.phone.value
document.addressform.email.value
Using Form Data
<form name="alertform">
Enter your name:
<input type="text" name="yourname">
<input type="button" value= "Go"
onClick="window.alert('Hello ' +
document.alertform.yourname.value);">
</form>
Language Elements
• Variables
• Literals
• Operators
• Control Structures
• Objects
19
JavaScript Variables
• Untyped!
• Can be declared with var keyword:
var foo;
20
Variables (cont.)
21
Literals
22
Operators
+ - * / % ++ -- == != > <
&& || ! & | << >>
23
Control Structures
• Again – pretty much just like C:
if if-else ?: switch
with (object)
24
Objects
25
Array Objects
• Attribute length
• Methods include:
concat join pop push reverse sort
26
Array example code
var a = [8,7,6,5];
for (i=0;i<a.length;i++)
a[i] += 2;
b = a.reverse();
27
Many other pre-defined object types
28
Predefined Objects
• JavaScript also includes some objects that are automatically created for
you (always available).
• document
• navigator
• screen
• window
29
The document object
30
document methods
string concatenation!
31
JavaScript Example
<HEAD>
<HEAD>
<TITLE>JavaScript
<TITLE>JavaScript isis Javalicious</TITLE>
Javalicious</TITLE>
</HEAD>
</HEAD>
<BODY>
<BODY>
<H3>I
<H3>I am
am aa web
web page
page and
and here
here is
is my
my
name:</H3>
name:</H3>
<SCRIPT>
<SCRIPT>
document.write(document.title);
document.write(document.title);
</SCRIPT>
</SCRIPT>
<HR>
<HR>
32
JavaScript and
HTML Comments
<SCRIPT> t
en
<!-- m
om
document.write("Hi Dave"); c
L
document.bgColor="BLUE"; T M
-->
H
</SCRIPT>
33
JavaScript Functions
function add(x,y) {
return(x+y);
}
34
JavaScript Events
35
Simple Event Example
<BODY
<BODY BGCOLOR=WHITE
BGCOLOR=WHITE onUnload="restore()">
onUnload="restore()">
<H5>Hello
<H5>Hello -- II am
am aa very
very small
small page!</H5>
page!</H5>
<SCRIPT>
<SCRIPT>
savewidth
savewidth == window.innerWidth;
window.innerWidth;
saveheight
saveheight == window.innerHeight;
window.innerHeight;
function
function restore()
restore() {{
window.innerWidth=savewidth;
window.innerWidth=savewidth;
window.innerHeight=saveheight;
window.innerHeight=saveheight;
}}
//
// Change
Change the
the window
window size
size to
to be
be small
small
window.innerWidth=300;
window.innerWidth=300; window.innerHeight=50;
window.innerHeight=50;
document.bgColor='cyan';
document.bgColor='cyan';
</SCRIPT>
</SCRIPT>
36
Buttons
• You can associate buttons with JavaScript events (buttons in HTML
forms)
<FORM>
<FORM>
<INPUT
<INPUT TYPE=BUTTON
TYPE=BUTTON
VALUE="Don't
VALUE="Don't Press
Press Me"
Me"
onClick="alert('now
onClick="alert('now you
you are
are in
in trouble!')“
trouble!')“ >>
</FORM>
</FORM>
37
Some Events (a small sample)
onUnLoad
onLoad Window events
onClick
onMouseUp Button events
onMouseDown
onDblClick
onMouseOver Link events
38
Document Object Model
• Naming hierarchy used to access individual elements of a HTML
document.
• Netscape D.O.M. is a little different than IE D.O.M. (D.A.M.)!!!*
• Easy to use if you name all entities:
• Forms, fields, images, etc.
40
Form Field Validation
• You can have JavaScript code that makes sure the user enters valid
information.
• When the submit button is pressed the script checks the values of all
necessary fields:
• You can prevent the request from happening.
41
Checking Fields
function
function checkform()
checkform() {{
if
if (document.myform.age.value
(document.myform.age.value ==
== "")
"") {{
alert("You
alert("You need
need to
to specify
specify an
an age");
age");
return(false);
return(false);
}} else
else {{
return(true);
return(true);
}}
}}
42
Nee
bro ded
ws to p
The Form er fr r
om event
sub the
mi t
<FORM METHOD=GET ACTION=foo.cgi ting
<FORM METHOD=GET ACTION=foo.cgi !
NAME=myform
NAME=myform
onSubmit="return(checkform())">
onSubmit="return(checkform())">
AGE:
AGE: <INPUT
<INPUT TYPE=TEXT
TYPE=TEXT NAME=Age>
NAME=Age>
<INPUT
<INPUT TYPE=SUBMIT>
TYPE=SUBMIT>
</FORM>
</FORM>
43
Quiz 1 (5%)