Javascript (Ecmascript) : Client-Side Dynamic Documents
Javascript (Ecmascript) : Client-Side Dynamic Documents
(ECMAScript)
Client-side dynamic documents
Netprog: JavaScript 1
Smart Browsers
• Most browsers support a <script> tag
that is used to include executable
content in an HTML document.
Netprog: JavaScript 4
Language Elements
• Variables
• Literals
• Operators
• Control Structures
• Objects
Netprog: JavaScript 5
JavaScript Variables
• Untyped!
• Can be declared with var keyword:
var foo;
Netprog: JavaScript 6
Variables (cont.)
• Using var to declare a variable results
in a local variable (inside a function).
Netprog: JavaScript 7
Literals
• The typical bunch:
– Numbers 17 123.45
– Strings "Hello Dave"
– Boolean: true false
– Arrays: [1,"Hi Dave",17.234]
Netprog: JavaScript 8
Operators
• Arithmetic, comparison, assignment,
bitwise, boolean (pretty much just like
C).
+ - * / % ++ -- == != > <
&& || ! & | << >>
Netprog: JavaScript 9
Control Structures
• Again – pretty much just like C:
if if-else ?: switch
with (object)
Netprog: JavaScript 10
Objects
• Objects have attributes and methods.
• Many pre-defined objects and object
types.
• Using objects follows the syntax of C++/
Java:
objectname.attributename
objectname.methodname()
Netprog: JavaScript 11
Array Objects
• Arrays are supported as objects.
• Attribute length
• Methods include:
concat join pop push reverse sort
Netprog: JavaScript 12
Array example code
var a = [8,7,6,5];
for (i=0;i<a.length;i++)
a[i] += 2;
b = a.reverse();
Netprog: JavaScript 13
Many other pre-defined object types
Netprog: JavaScript 14
Predefined Objects
• JavaScript also includes some objects
that are automatically created for you
(always available).
– document
– navigator
– screen
– window
Netprog: JavaScript 15
The document object
• Many attributes of the current
document are available via the
document object:
Title Referrer
URL Images
Forms Links
Colors Cookies
Netprog: JavaScript 16
document methods
• document.write() like a print
statement – the output goes into the
HTML document.
string concatenation!
Netprog: JavaScript 17
JavaScript Example
<head>
<title>JavaScript is Javalicious</title>
</head>
<body>
<h3>I am a web page and here is my
name:</h3>
<script>
document.write(document.title);
</script>
<hr/>
Netprog: JavaScript 18
JavaScript and
HTML Comments
<script>
n t
<!-- e
m
document.write("Hi Dave"); om
c
L
document.bgColor="BLUE"; T M
H
-->
</script>
Netprog: JavaScript 19
JavaScript Functions
• The keyword function used to define
a function (subroutine):
function add(x,y) {
return(x+y);
}
Netprog: JavaScript 20
JavaScript Events
• JavaScript supports an event handling
system.
– You can tell the browser to execute
javascript commands when some event
occurs.
– Sometimes the resulting value of the
command determines the browser action.
Netprog: JavaScript 21
Simple Event Example
Depending on your browser setting, this may not change the window size...
<body onUnload="restore()">
<h5>Hello - I am a very small page!</h5>
<script>
savewidth = window.innerWidth;
saveheight = window.innerHeight;
function restore() {
window.innerWidth=savewidth;
window.innerHeight=saveheight;
}
// Change the window size to be small
window.innerWidth=300; window.innerHeight=50;
document.bgColor='cyan';
</script>
Netprog: JavaScript 22
Buttons
• You can associate buttons with JavaScript
events (buttons in HTML forms)
<form>
<input type='button'
value="Don't Press Me"
onClick="alert('now you are in trouble!')“ >
</form>
Netprog: JavaScript 23
Some Events (a small sample)
onUnload
Window events
onLoad
onClick
onMouseUp Button events
onMouseDown
onDblClick
onMouseOver Link events
Netprog: JavaScript 24
Document Object Model
• Naming hierarchy used to access
individual elements of a HTML
document.
• There are some variations in the DOM
supported by browsers (DAM!!!)*
• Easy to use if you provide id attributes
for all entities:
– Forms, fields, images, etc.
Things are getting better all the time – there are
standard DOMs defined by The W3C
Netprog: JavaScript 25
DOM example
<form id=myform ACTION=…
Please Enter Your Age:
<input type='text' id='age'
name='age'><br/>
And your weight:
<input type='text'id='weight'
name='weight'><br/>
</form>
From javascript you can get at the age input field as:
document.getElementById("age").value
Netprog: JavaScript 26
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.
Netprog: JavaScript 27
Checking Fields
function checkform() {
if
(document.getElementById("age").value
== "") {
alert("You need to specify an age");
return(false);
} else {
return(true);
}
Netprog: JavaScript 29
Complete Form
Example
Netprog: JavaScript 30
Important Note about Form
Validation!!!
• It's a good idea to make sure the user fills out
the form before submitting.
Netprog: JavaScript 31
Lots of JavaScript
• There are many javascript examples
available via the course home page:
Netprog: JavaScript 32