Professional Documents
Culture Documents
Lecture 12
Instructor: Ms. Mehak Sheikh
1
Introduction to JavaScript
2
Outline
• JavaScript functions
• Conditional Statements
• Looping Statements
• Arrays
3
7. JavaScript Function
• User defined functions
• Predefined functions
4
7. JavaScript Function…
• Functions are defined using the keyword
function, followed by the name of the
function and list of parameters
function functionName([parameters])
{
[statements]
}
5
7. JavaScript Function…
• Calling a function
– The syntax of a function call is:
functionName([arguments])
6
7. JavaScript Function…
Asks user to
enter name
Writes name on
the webpage
Calling a function
7
7. JavaScript Function…
• Common events
• onClick
• onDblClick
• onSubmit
• onload
8
7. JavaScript Function…
• Some common predefined math functions
• Math.sqrt
• Math.pow
• Math.abs
• Math.max
• Math.min
• Math.floor
• Math.ceil
• Math.round
• Math.random
9
8. Conditional Statements
• If statement
• if (condition)
statement
• if(condition)
{ statements }
• If-else statement
• if(condition)
{statement}
else
{statements}
10
8. Conditional Statements…
User’s Input
If condition
11
8. Conditional Statements…
text
On click event
12
9. Loops
• For loop
for(var i=1; i<10; i++)
{
Document.write(“hello world”)
}
• While loop
While(condition)
{
Set of statements
}
i=0;
While (i<=3)
{
Document.write(“hello world”);
i++;
}
13
9. Loops
For loop
Do-while loop
14
9. Loops
Output of do-
while loop
15
1. Dialog boxes in JavaScript
• JavaScript provides the ability to pickup user
input or display small amounts of text to the
user by using dialog boxes
• These dialog boxes appear as separate windows
and their content depends on the information
provided by the user
16
1.1 Alert Box
• An alert box is simply a small message box that
pops up and gives the user some information
• An alert dialog box is mostly used to give a
warning message to the users
• When an alert box pops up, the user will have to
click "OK" to proceed
• Syntax:
– alert(“message”)
17
1.1 Alert Box…
18
1.2 Prompt box
• A prompt box is often used if you want the user
to input a value before entering a page
• When a prompt box pops up, the user will have
to click either "OK" or "Cancel" to proceed after
entering an input value
• If the user clicks "OK" the box returns the input
value
• If the user clicks "Cancel" the box returns null
19
1.2 Prompt box…
Prompt box
20
1.3 Confirm box
• A confirm box is often used if you want the
user to verify or accept something
• When a confirm box pops up, the user will
have to click either "OK" or "Cancel" to
proceed
• If the user clicks "OK", the box returns true
• If the user clicks "Cancel", the box returns
false
21
HTML DOM
22
2. Document Object Model (DOM)
• To create interactive web pages it is vital that
the browser continues to recognize HTML
elements
• JavaScript enabled browsers do this because
they recognize and uses DOM
23
2. Document Object Model (DOM)…
24
2. Document Object Model (DOM)…
25
2. Document Object Model (DOM)…
Window
Textboxe
Element[
of forms
element
select
Reset
li
]
files
list
26
2.1 Retrieving HTML elements
• The getElementById() used to return the
element that has the ID attribute with the
specified value. This is one of the most
common methods in the HTML DOM and is
used almost every time we want to manipulate
an element on our document.
• To retrieve an element, it must have an
unique id
– document.getElementById(“element-id”)
27
2.1 Retrieving HTML elements…
• The returned reference can be used to
retrieve element attributes
– document.getElementById(“element-
id”).attribute
– document.getElementById(“pic”).src(ref)
– document.getElementById(“pic”).height
28
2.2 Retrieving the text of an element
29
2.3 Getting value of attributes
• getAttribute() method is used to retrieve values
of attributes
– document.getElementById(“element-
id”).getAttribute(“attribute-name”)
– document.getElementById(“pic”).getAttribute(“src”)
30
2.4 Setting value of attributes
• setAttribute() method is used to set values of
attributes
– document.getElementById(“element-
id”).setAttribute(“attribute-name”, ”Value”)
– For example
– document.getElementById(“element-
id”).setAttribute(“class”, ”class1”)
31
References
• Chapter 11. Beginning HTML,
XHTML,CSS, and JavaScript, by Jon
Duckett, Wiley Publishing; 2009, ISBN:
978-0-470-54070-1.
• Chapter 2,3, Learn JavaScript, by Chuck
Easttom, Wordware Publishing; 2002,
ISBN 1-55622-856-2
32