Client Side Programming
Client Side Programming
of Data on the Web There are cases where we cannot choose where
to perform the computation
For example, accessing a database
Applets Limitations
Security Restrictions: Applets cannot access files
in the client computer nor files (or databases)
behind a firewall
The Bandwidth Problem: Applets are usually large
in comparison to HTML files, thus , the download
time becomes unacceptable
Compatibility:
Client must have a compatible browser
Thin clients may not support the whole Java API
Overview
A "scripting" language for HTML pages
The code is embed in HTML pages
Javascript The browser interprets and executes the script (it is
not compiled)
Do not declare data types for variables (loose
typing)
Dynamic binding object references checked at
runtime
It is not Java Web Architecture for JavaScript
"CLIENT" "SERVER"
Desktop access Remote host
Java : Web browser
Web
(HTTP)
compilation required (not a script) HTML Page: HTML/HTTP HTML/HTTP Server
<SCRIPT> Internet
TCP/IP TCP/IP
can create stand alone application </SCRIPT>
object-oriented
:K\GRZH
Example QHHGEU!LIZH
XVHGZULWHOQ"
<HTML>
<HEAD><TITLE>An Hello World Example</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
document.write("<font size='+4'>");
document.writeln("Hello World!<br>");
document.writeln("What a boring example</font>")
</SCRIPT>
</HEAD>
<BODY> <!-- An empty document body --> </BODY>
</HTML>
Example
<HTML>
<HEAD><TITLE>An Hello World Example</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
document.write("<font size='+4'>");
document.writeln("Hello World!<br>");
document.writeln("What a boring example</font>")
</SCRIPT>
</HEAD>
<BODY> <h1>My Hello World Example</h1> </BODY>
</HTML>
Some of Javascript Reserved
Example
<HTML>
Keywords
<HEAD><TITLE>An Hello World Example</TITLE> break function true
<SCRIPT LANGUAGE = "JavaScript">
document.write("<font size='+4'>");
case if typeof
document.writeln("Hello World!<br></font>"); continue in var
</SCRIPT>
delete new void
</HEAD>
<BODY> <h1>My Hello World Example</h1> do null while
<SCRIPT LANGUAGE = "JavaScript">
else return with
document.writeln("What a boring example")
</SCRIPT> false switch
</BODY> for
:KDWZLOOKDSSHQ"
this
</HTML>
enum val = 1;
LQWWRD6WULQJ
Javascript is case sensitive (foo and FOO are Local variables must be declared with var
different)
Literals Operators
Which of the following two is correct? Which of the following two is correct?
What is the type of the answer? What is the type of the answer?
The equals == checks if both operands are The equals != checks if both operands are
equal after performing type conversion unequal after performing type conversion
The equals === checks if both operands are The equals !== checks if both operands are
of the same type and equal not of the same type and or not equal
Example: Example:
Is 3 == "3" (true or false?) Is 3 != "3" (true or false?)
Is 3 === "3" (true or false?) Is 3 !== "3" (true or false?)
Conditional Operators Boolean Operators
equals and
if (a == b) { }
if (true && true) { }
not equals
or
if (a != b) { }
if (true || false) { }
greater than or equal to
if (a >= b) {...} not
less than or equal to if (! false) {...}
if (a <= b) {...}
<HTML>
<HEAD><TITLE>Using Variables</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
var firstNumber = 11,
secondNumber = 23,
sum;
sum = firstNumber + secondNumber;
document.write("<FONT COLOR = 'blue' SIZE = '6'>The sum
of " + firstNumber + " and " + secondNumber + " is
</FONT>");
document.write(" <FONT COLOR = ¶red' SIZE = '7'>" + sum
+ "</FONT>");
</SCRIPT>
</HEAD>
<BODY> <!-- An empty document body --> </BODY>
</HTML>
And a few not like in Java var metushelahAge = 130; var yourAverageAge
yourAverageAge = metushelahAge - 98
for (var in object)
var myObject = new Object("initial value")
with (object) more statements here..
«..
JavaScript Constructs JavaScript Constructs
condition (selection) loop (iteration): both for and while loops are
if (condition) {statements if true} available
else {statements if false}
for (var i=0; i < inputAge.length; i++) {
if (metushelahAge < yourAverageAge) { var oneChar = inputAge.substring (i, i+1)
document.write ("<body><h2>its true that if (oneChar < "0" || oneChar > "9") {
Metushelah is younger than most of you,") alert("Please enter a valid number ´
document.write ("<br>computers never lie!</h2> + oneChar + " is not valid.")
</body>") }
}
}
<HTML>
<HEAD><TITLE>Loops Example</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
</FONT></P> ´);
}
</SCRIPT>
</HEAD>
<BODY> <!-- An empty document body --> </BODY>
</HTML>
Functions are first class citizens Numbers and Boolean values always
The keyword function used to define a passed to functions using call-by-value
function (subroutine): For objects, a call-by-reference is used to
pass them to the functions
function add(x,y) { Numbers and Boolean values are always
return(x+y); returned by value
} Objects returned by reference
Example
<HTML> <BODY>
<HEAD><TITLE>Functions Example</TITLE> <FORM NAME = "fibonacciForm">
<SCRIPT LANGUAGE = "JavaScript"> <TABLE BORDER = "1" BGCOLOR = ´fabbfc">
function fibonacciValue() { <TR><TD BGCOLOR = ´eabbfc">Enter a number</TD>
var value = parseInt(document.fibonacciForm.number.value ); <TD><INPUT NAME = "number" TYPE = "text"></TD>
window.status = "Calculating Fibonacci number for " + value; <TD><INPUT TYPE = "button" VALUE = "Calculate"
document.fibonacciForm.result.value = fibonacci(value); ONCLICK = "fibonacciValue()"</TR>
window.status = "Done Calculating Fibonacci number"; <TR><TD BGCOLOR = ´fabbfc">Fibonacci Value</TD>
} <TD><INPUT NAME = "result" TYPE = "text"> </TD>
function fibonacci( n ) { </TR>
if (n == 0 || n == 1) return n; </TABLE>
else return fibonacci( n - 1 ) + fibonacci( n - 2 ); </FORM>
} </BODY>
</SCRIPT></HEAD> </HTML>
Global Functions Objects
Javascript include several predefined functions that Objects have attributes and methods
you can use
There are pre-defined objects and user-
For example,
defined object types
eval(code-string) gets a string of JavaScript
code, evaluates it and executes it Using objects has similarity to the syntax of
It allows dynamic code execution C/Java:
parseInt(string) takes a string argument and
converts its beginning to an integer number (or return
objectName.attributeName
NaN) objectName.methodName()
Constructor Function:
define a constructor function theMazda = new car( Mazda", 3 23", 1991)
theHonda = {make: Honda , year:1992,
create the new object using new color:"red",wheels:4,
engine:{cylinders:4,size:2.2}}
Creating a Method Example
Array Example Code Understanding Arrays
Array literals:
arr1 = ["hello", 1, 33]
var a = [8,7,6,5];
Accessing arrays: (what is the result of)
document.writeln(arr1.length)
for (i=0;i<a.length;i++)
document.writeln(arr1[0])
a[i] += 2;
document.writeln(arr1["abc"])
b = a.reverse(); arr1[10]=66
document.writeln(arr1.length)
<HTML><HEAD><TITLE>Arrays Example</TITLE>
Passing Arrays to Functions <SCRIPT LANGUAGE = "JavaScript">
function start() {
var colors = ["red", "blue", "green"]
Arrays can be passed to functions as
printArray(colors);
arguments printArray(colors);
}
The array is passed by call-by-reference function printArray( arr) {
for (var i in arr) {
The name of the array is given to the
document.writeln("<FONT SIZE=5 COLOR=" + arr[i] + ">"
function + arr[i] + " </FONT><BR>");
arr[i] = "gray";
}
} </SCRIPT>
</HEAD><BODY ONLOAD = "start()"> </BODY></HTML>
Multidimentional Arrays
Other Object Types Math Common Methods
<HTML>
<HEAD><TITLE>Time Example</TITLE> <BODY>
<SCRIPT LANGUAGE = "JavaScript"> <FORM NAME="timesForm">
function getTimes() { <P>
var current = new Date();
<INPUT NAME = "getTimeButton" TYPE = "button"
var out = "Day: " + current.getDay()+"\n";
VALUE = "Get Time" ONCLICK = "getTimes()">
out = out.concat("Month: " + current.getMonth() + "\n");
<P>
out = out.concat("Year: " + current.getFullYear() + "\n");
out = out.concat("GMT Time: " + current.toUTCString() + <TEXTAREA NAME = "output" ROWS ="10"
"\n"); COLS="42">
out = out.concat("Time: " + current.toString() + "\n"); </TEXTAREA>
document.timesForm.output.value = out; </FORM>
}
</BODY>
</SCRIPT>
</HTML>
</HEAD>
Predefined Objects
Many attributes of the current document are JavaScript objects include object hierarchy +
(property or method)
available via the document object:
window.document.lastModified
title cookie window.document.clear()
URL images need not be fully qualified
forms links document.lastModified
Example of with Dynamic HTML
var a, x, y
Java Java
var r=10 CSS Script Script
with (Math) {
HTML CSS
a = PI * r * r HTML HTML
x = r * cos(PI) HTML
y = r * sin(PI/2)
}
Dynamic HTML