Browser Scripting: Client-Server Architecture Client Side Technologies
Browser Scripting: Client-Server Architecture Client Side Technologies
JavaScript
1 2
About Applets
1
Browser Scripting Web Architecture for Scripts
Client Server
• Browser scripts are procedural programs
Web browser
embedded inside HTML Web
(HTTP)
HTML Page: HTML/HTTP HTML/HTTP
Server
<script type="text/javascript">script</script> <SCRIPT>
…code..…
TCP/IP
Internet
TCP/IP
</SCRIPT>
<script type="text/vbscript">script</script>
• Can read and manipulate HTML elements, CSS
properties, and the browser itself built-in
Script HTML
interpreter pages with
embedded
script
7
• Interaction among the frames and windows of the equivalent to JavaScript 1.5
browser
9 10
2
Dynamic HTML Content:
Example 1
<html>
<head><title>JS Example</title></head> <body>
<h2>Before the script</h2>
<script type="text/javascript">
document.write('<h1>In the script<\/h1>')
</script>
<h2>After the script</h2>
</body></html>
13 14
3
Data Types Some of the Reserved Words
• Logical: & && | || ! • The equals === checks if both operands are of
• Bitwise: & | ^ ~ << >> >>> the same type and equal
• String: + • Example:
- Is 34 == "34" ? Is 34 == "3"+"4" ?
• Assignments: = += -= *= /= <<= |= ...
- Is 34 === "3"+"4" ? Is 34 !== "3"+"4" ?
21 22
An Example
<script type="text/javascript">
</script>
23 24
4
Functions
25 26
• Function may receive arguments without declaring them What is the result of the following code?
• Within a function, its arguments are held in the function myConcat(separator) {
var result="";
arguments array // iterate through arguments
for (var i=1; i<arguments.length; i++) {
- can be accessed with arguments[i] result += arguments[i] + separator;
}
- The number of arguments is arguments.length return result;
• Hence, it is possible to define functions that take any }
29 30
5
Predefined Functions Variable Scopes
• JavaScript include several predefined functions • JavaScript variables are recognized inside their
• For example, declaration scope
- eval(code-string) – gets a string of JavaScript code, • Hence, global variables should be declared
evaluates it and executes it outside the functions
• It allows dynamic code execution
• A variable declared in a function can also be
- parseInt(string) – takes a string argument and global, if var is omitted
converts its beginning to an integer (or return NaN)
- However, avoid this bad style...
31 32
Object Model
33 34
6
Defining Methods Defining Methods (cont)
• Methods are associated with objects just like attributes theNissan = {make:"Nissan", year:2003, color:"blue"}
function niceString() { theNissan.str = niceString;
return "<span style='color:"+ this.color + "'>" +
this.make + " "+ this.year + "<\/span>" function car(make,year,color) {
} this.make = make
this.year = year
this.color = color
theNissan = this.str = niceString
{make:"Nissan",year:2003,color:"blue",str:niceString} }
37 38
41 42
7
Array Elements
47 48
8
An Example - Format Verification The Math Object
• What does the following function do? • The object Math is used for mathematical operations
function getString() { - E.g., Math.pow(x,2)
var result = null;
while(result==null) { • Other useful functions:
var answer = prompt("Your first name:") • abs(x) • cos(x) • pow(x, y)
if(answer!=null) {
• round(x) • sin(x) • sqrt(x)
result = new String(answer);
result = result.toLowerCase().match("^[a-z]+$"); • ceil(x) • tan(x) • log(x)
} • floor(x) • exp(x) • max(x, y)
if(result==null) { alert("Don't mess with me!") }
• min(x, y)
}
return answer } 49 • Math Also includes constants such as: Math.E, Math.PI 50
• Establishes the default object for a set of • Create the current date: new Date()
statements • Create an arbitrary date: new Date(date-string)
• Within the set of statements, any property • Common methods of Date:
references that do not specify an object are • getDate() • getHours()
assumed to be of the default object • getFullYear() • getMinutes()
var a, x, y • getMonth() • getSeconds()
var r=10
• getDay • getMilliseconds()
with (Math) {
a = PI * r * r; x = r * cos(PI); y = r * sin(PI/2) } 51
• getTime() • toLocaleString() 52