CS1113 Web Programming: Client Side Scripting
CS1113 Web Programming: Client Side Scripting
Lecture 11
Client Side Scripting
(JavaScript - I)
1
In Todays Lecture
We will get our first taste of JavaScript the
object-based language.
Today we will write (simple) client-side scripts in
JavaScript.
We will become able to appreciate the concept of
objects in JavaScript.
We will learn about the properties & methods of
those objects, and how to read & modify them.
2
HTML
CSS
JavaScript
markup
conten
language
style sheet
presentatio
language
programming
language
behavior
Info contained
in the form
Browser
Users
Computer
Ack
no
wle
dg
Server-Side
Script
eme
nt
Web
Server
Message
to the
receivers
eMail
address
A Reasonable Scenario
When the Send eMail button is clicked, the
browser sends the data collected through the
form to a script running on the Web server
That server-side script:
receives that data
analyzes it
discovers the missing or incorrect data
sends a message back to the users browser
stating the problem and asks the user to re-send
10
This process
That is the process of user:
Filling the incomplete/incorrect data
Sending it to the server
Receiving the response back from the server
Correcting and resending
Users Computer
Browser
HTML
HTML
CSS
CSS
JavaScript
15
16
Disadvantages
Client-side scripts do not work with all browsers
Some user intentionally turn scripting off or
disabled on their browsers
This increases the complexity of the Web page,
as it now has to support both situations:
browsers with scripting capability, and those not
having that capability
17
-- Modify
-- Delete
19
COOKIES
Cookies are small files, in which browser
can store some data on client machine, so
that next time user visit the same webpage.
The webpage get to know where he is
actually.
Example:
In cookies, there is information about client
machine address, name, time and date.
When i visit amazon.com for book purchase,
it is written on the webpage, hello
Yasir.
21
Java App
JavaScript
.Net App
A Simple Example of
Client-Side Scripting
23
24
<INPUT
type=submit
name=sendEmail
value=Send eMail
>
<INPUT
type=submit
name=sendEmail
value=Send eMail
onMouseOver=
if (document.sendEmail.sender.value.length < 1)
window.alert(Empty From field! Please correct)
>
<INPUT
Event
type=submit
Handler
name=sendEmail
value=Send eMail
onMouseOver=
if (document.sendEmail.sender.value.length < 1)
window.alert(Empty From field! Please correct)
>
27
Why JavaScript?
HTML is great for static Web pages; however,
supports only rudimentary interactivity through
forms and hyperlinks
JavaScript can be used (along with HTML) to
develop interactive content for the Web
29
What is JavaScript?
A programming language specifically designed
to work with Web browsers
It is designed to be used for developing small
programs called scripts that can be
embedded in HTML Web pages
JavaScript:
Is an interpreted language
Supports event-driven programming
Is object-based language
30
Object Based?
Everything that JavaScript manipulates, it
treats as an object e.g. a window or a button
An object has properties e.g. a window has
size, position, status, etc.
Properties are modified with methods e.g. a
resize a window with resizeTo(150, 200)
31
Not Object-Oriented!
JavaScript is not a true object-oriented
language like C++ or Java
It is so because it lacks two key features:
A formal inheritance mechanism
Strong typing
33
34
<INPUT
type=submit
name=sendEmail
value=Send eMail
onMouseOver=
if (document.sendEmail.sender.value.length < 1)
window.alert(Empty From field! Please correct)
>
35
<INPUT
type=submit
name=sendEmail
value=Send eMail
onMouseOver=checkForm()
>
36
<INPUT
type=submit
name=sendEmail
value=Send eMail
onMouseOver=
if (document.sendEmail.sender.value.length < 1)
window.alert(Empty From field! Please correct)
>
<INPUT
type=submit
name=sendEmail
value=Send eMail
onMouseOver=checkForm()
37
>
checkForm()
JavaScript understands onMouseOver it is
one of the pre-defined keywords in JavaScript
However, the case for checkForm() is different
A checkForm() function does not exist in
JavaScript. Therefore, we will have to define it
ourselves
It can either be defined in the HEAD portion or
BODY portion. We will do it in the HEAD.
38
<HTML>
<HEAD>
<TITLE>Send an eMail</TITLE>
<SCRIPT type=text/javascript">
function checkForm() {
if ( document.sendEmail.sender.value.length < 1) {
window.alert( Empty From field! Please correct ); }
}
</SCRIPT>
</HEAD>
<BODY bgcolor=#FFFFCC>
body content
JavaScript code
</BODY>
enclosed in the new
</HTML>
<SCRIPT>,</SCRIPT>
39
HTML tags
42
Client-Side JavaScript
Although a version of JavaScript exists
that can be used to write server-side
scripts, our focus in this course will only
be on client-side scripting
45
Case Sensitivity
HTML is not case sensitive. The following
mean the same to the browser:
<HTML>
<Html>
-- <html>
-- <htMl>
alert(hello world);
Alert(hello world);
47
prop 1
prop 2
method 1
prop 3
prop 4
method 2
prop 5
method 3
48
Example: A Bicycle
color
name
height
inflate()
pressure
speed
accelerate()
direction
turn() park()
49
width
height
close()
name
document
location
open()
status
alert()
50
Properties
Objects may have a single or several properties
A property may have one of the following
values:
Number
-- Text
-- Boolean
Array
-- Functions
Objects (Example: document a property of the
window object is an object in itself. A
document in turn may contain a form object as a
property, and then that form may contain a
button property, which, once again, is an object in
itself)
51
Referring to a Property
dot
objectName.propertyName
Examples:
window.width
button.value
52
53
54
<HTML>
<HEAD>
<TITLE>Change Property Demo # 1</TITLE>
<SCRIPT type=text/JavaScript>
function changeStatus() {
window.status = Mouse has touched the button;
}
</SCRIPT>
</HEAD>
<BODY>
<H1>Change Property Demo # 1</H1>
<FORM name=dummy method= action=>
<INPUT type=submit name= value=Change Status
onMouseOver=changeStatus()>
</FORM>
</BODY>
55
</HTML>
property
57
58
property
60
A Suggestion
Please try the pieces of code that I just
demonstrated to you to change the
status and location properties of the
window object yourself
Also try changing the width, height
properties of the window object
61
Types of Objects
JavaScript objects
Objects that are part of JavaScript
Examples: window, document
Browser objects
Objects that contain info not about the
contents of the display, but the browser itself
Examples: history, navigator
User-defined object
62
63
Applet
Area
Array
Boolean
Button
Checkbox
Date
Document
Event
Fileupload
Form
Frame
Function
Hidden
History
Image
Layer
Link
Location
Math
Mimetype
Navigator
Number
Object
Option
Password
Plugin
Radio
RegExp
Reset
Screen
Select
String
Submit
Text
Textarea
window
64
Referring to a Method
dot
objectName.methodName( argumnets )
Examples:
window.close()
button.click()
67
Info is
passed on to
the method
through one
or more
arguments
alert()
confirm()
prompt()
close()
open()
focus()
blur()
setTimeOut()
setInterval()
68
69
method
html
head
body
title
72
h1
ul
li
li
li
Retrieving an element by ID
var myElement = document.getElementById(abc);
html
head
title
body
h1
ul id =abc
li
li
74
li
75
Retrieving an element by ID
var ListItems = document.getElementsByTagname(li);
var myLinks = document.getElementsByTagname(a);
html
myLinks
head
ListItems
0
1
2
title
body
h1
ul id=abc
li
li
76
li
77
78
property
fgColor
linkColor
title
URL
referrer
lastModified
cookie
forms[ ]
images[ ]
links[ ]
80
Popup Boxes
Alert Box
window.alert(Hello Every body!!");
Confirm Box
window.confirm("Press a button");
Prompt Box
window.prompt(Your name","")
81
82
Read-Only Properties
83
85
86
Event Handlers
Objects are made up of properties and
associated methods
Many objects also have event handlers
associated with them
Events are actions that occur as a result of
users interaction with the browser
We use event handlers [e.g. onMouseOver(),
onClick()] to design Web pages that can react
to those events
88
More on event handlers in a future
lecture