Professional Documents
Culture Documents
Week 5 - Webapps
Week 5 - Webapps
JavaScript
Week 5
WEB APPLICATIONS DEVELOPMENT - CIT
Learning Outcomes
1. Know what Javascript is and its use.
2. Know where to place Javascript in the HTML documents.
3. Discuss the (Document Object Model) DOM Elements
4. Learn the JavaScript conditional statements.
3
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
What is JavaScript?
WEB APPLICATIONS DEVELOPMENT - CIT
JavaScript
- JavaScript is the world’s most popular programming language.
- JavaScript is a scripting language that enables you calculate,
manipulate data on the web pages.
- JavaScript is the programming language of the Web.
- JavaScript developed by NetScape, Inc. is not part of Java. Java is
owned by Suns Microsystems, Inc.
- JavaScript is created by Brendan Eich of Netscape, Inc. in 1995.
5
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
6
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
Where to place Javascript?
WEB APPLICATIONS DEVELOPMENT - CIT
<script>
document.getElementById(“demo”).innerHTML = “My first JavaScript”
</script>
8
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
// myScript.js
document.getElementById(“demo”).innerHTML = “My first JavaScript”
<script src=”myScript.js”></script>
9
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
10
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
JavaScript Output
WEB APPLICATIONS DEVELOPMENT - CIT
JavaScript Output
JavaScript can “display” data in different ways:
- Writing into an HTML element, using innerHTML.
- Writing into the HTML output using document.write().
- Writing into an alert box, using window.alert().
12
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
Cont…
- Using document.write() after an HTML document is loaded, will delete
all existing HTML:
13
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
JavaScript (Document Object
Model) DOM Elements
WEB APPLICATIONS DEVELOPMENT - CIT
- With the HTML DOM, JavaScript can access and change all the
elements of an HTML document.
15
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
16
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
document.getElementById(‘demo’).style.textAlign = “center”;
document.getElementById(‘demo’).style.fontSize = “32px”;
document.getElementById(‘demo’).style.textAlign = “center”;
document.getElementById(‘demo’).style.display = “none”;
17
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
document.getElementById(“demo”).addEventListener(“click”, function() {
window.alert(“Hello World”);
});
18
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
Click event
- The click event activates when the element is clicked.
document.getElementById(“button”).addEventListener(“click”, function() {
window.alert(“Hello World”);
});
19
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
20
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
Change event
- The change event activates when the element is change.
document.getElementById(“select”).addEventListener(“change”, function() {
});
21
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
Key event
- The key event activates when the element is change.
document.getElementById(“input”).addEventListener(“keydown”, function() {
});
22
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
JavaScript Conditional
Statements
WEB APPLICATIONS DEVELOPMENT - CIT
Conditional Statements
- Conditional statements are used to perform different actions based on
different conditions.
- Very often when you write code, you want to perform different actions
for different decisions.
24
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
Cont…
- In JavaScript we have the following conditional statements:
- Use if to specify a block of code to be executed, if condition is true.
- Use else to specify a block of code to be executed, if condition is
false.
- Use else if to specify a block of code to be executed, if the first
condition is false.
25
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
The if statement
- Use the if statement to specify a block of JavaScript code to be
executed if a condition is true.
Example: Display Minor age’ if the age is less than 18 years old.
if (age < 18) {
message = “Minor age”;
}
26
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
Example: If the age less than 18 years old prints a “Minor age” message, otherwise “Legal age”.
if (age < 18) {
message = “Minor age”;
} else {
message = “Legal age”;
}
27
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN
WEB APPLICATIONS DEVELOPMENT - CIT
Example: If the age less than 18 years old prints a “Minor age” message, but age greater than 18 years
old prints a “Legal age” message, otherwise “Invalid age”.
if (age < 18) {
message = “Minor Age”;
} else if (age > 18) {
message = “Legal age”;
} else {
message = “Invalid age”;
}
28
SYSTEMS PLUS COMPUTER COLLEGE - CALOOCAN