Java Script
Java Script
Java Script
TBS/2019-2020
Introduction
2
JavaScript is
NOT Java: the similarity in syntax is due to the C language.
a scripting language : lightweight programming language.
the most popular scripting language on the Internet.
an interpreted language : the scripts execute without
preliminary compilation.
executed on the client side: the browser.
usually embedded directly into web pages.
How to Insert Javascript? (1/3)
4
page.html
Result
How to Insert Javascript? (3/3)
External: Outside the HTML page
6
page.html
script.js
Result
Remarks
7
var x, y, z; // Statement 1
x = 5; // Statement 2
y = 6; // Statement 3
z = x + y; // Statement 4
Remark:
Keywords are NOT case sensitive in javascript
e.g.: VAR , var and Var are the same keyword
JavaScript Operators (1/4)
12
Arithmetic Operators
Operator Description Example Result
+ Addition x=2 4
y=2
x+y
Operators allow operation on
- Subtraction x=5 3
two or more values. y=2
Assignment Operators
Operator Example Is The Same As
/= x/=y x=x/y
%= x%=y x=x%y
JavaScript Operators (3/4)
14
Logical Operators
Operator Description Example
y=3
(x==5 || y==5)
returns false
! not x=6
y=3
!(x==y) returns
true
JavaScript Basic Examples
16
<script>
document.write("Hello World!")
</script>
<script>
alert("Hello World!")
</script>
JavaScript Basic Examples
17
<script>
x=“Hello World!”
document.write(x)
</script>
<script>
x=“World”
document.write(“Hello” +x)
</script>
</body>
</html>
Dialog Boxes
19
■ window.alert( )
message
Example:
window.alert("message"); OK
OK
Insert your name
cancel
■ window.prompt( )
Example:
var nom=window.prompt(“Insert your name:”);
Example: OK cancel
Alert Box :
Syntax: window.alert(..) or alert(..)
<script>
alert("Hello World!")
</script>
JavaScript Popup Boxes (2/3)
21
Prompt Box
Syntax: window.prompt(..) or prompt(..)
Confirm Box
Syntax: window.confirm(..) or confirm(..)
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition1 is false and condition2 is true
}
else {
code to be executed if condition2 is false
}
Conditional Statements (3/3)
25
<script>
var x=prompt(" Enter a number ");
if(x<0)
{
alert ("You entered a negative number");
}
else
{
alert ("You entered a positive number");
}
</script>
JavaScript – When is it Executed?
26
<html> image-onclick.html
<head>
<script type="text/javascript">
function test (message) {
alert(message);
}
</script>
</head>
<body>
<img src="logo.gif"
onclick="test('clicked!')" />
</body>
</html>
Document Object Model (DOM)
29
Syntax
document.getElementById(id).attribute = new value
Example
<!DOCTYPE html>
<html>
<body>
<img id="myImage" src=”scenery.gif">
<script>
document.getElementById("myImage").src = "sunrise.jpg";
</script>
</body>
</html>
Changing HTML Style
35
Syntax
document.getElementById(id).style.property = new style
Example
<!DOCTYPE html>
<html>
<body>
<p id="demo">Hello World!</p>
<script>
document.getElementById("demo").style.color = "blue";
</script>
<button type="button"
onclick="document.getElementById('demo').style.fontSize = '50px’ ">
click me</button>
</body>
</html>
Exercise 1
36