CH 4
CH 4
Chapter Four
JavaScript
By
Tesfahun
Outline
✓ Introduction to JavaScript
✓ JavaScript Basic
▪ JavaScript Syntax
▪ Attaching JavaScript to HTML (External,
Embedded, Inline)
✓ JavaScript Comments
Sheets)
➢ It embedded in HTML
➢ It is Client-side technology
What is JavaScript?
➢ JavaScript allows for interactivity
➢ Browser/page manipulation
to websites
Cont..
➢JavaScript code can be inserted anywhere within the
HTML of a webpage
➢Eg
➢JavaScript function may check a web form before it is
submitted to make sure all the required fields have
been filled out.
❖Better performance
➢ it validates the data that users enter into the
form, before it is sent to your Web
application.
what JavaScript do?
➢ To handle events
➢ To handle exceptions
➢ Perform calculations
How Does It Work?
❖ Embedded within HTML page
❖ Executes on client machine
➢ Fast, no connection needed once loaded
➢ Files
➢ Programs
➢ Databases
❖ When you are using sensitive or copyrighted data
or algorithms.
➢You have to use the type attribute within the <script> tag
and set its value to text/javascript like this:
➢ <script type="text/javascript">
Cont..
➢ Example
<html>
<head>
</script>
</head>
<body>
</body>
</html>
Cont..
Example 2
<script>
function sum(a,b)
return a + b;
}
var total = sum(7,11);
alert(total);
</script>
Embedding JavaScript in HTML
1. Language
<script language = "JavaScript1.2"> ..</script>
1. Embedding code
2. Inline code
3. External file
Cont..
➢ I. Embedding code:-
<html>
<head>
< t i t l e > page t i t l e < / t i t l e >
<script>
document.write("Welcome t o J a v a t p o i n t " ) ;
</script>
</head>
<body>
…..
</body>
</html>
Cont..
2. inline code:-
➢ It also saves us from the task of writing the same code over and over
again and makes it easier to maintain web pages.
…
external JavaScript
function sample() {
a l e r t ( ' H e l l o from sample.js!')
}
JavaScript Comments
❖ JavaScript Output
document.getElementById(id) method.
➢ <body>
<h1>My F i r s t Web Page</h1>
<p>My F i r s t Paragraph</p>
<p id="demo"></p>
<script>
documen t. getElementByI d ( "d emo") . i nnerHTML = 5 + 6 ;
</script>
</body>
Cont.…
Using document.write()
<button type="button"
document.write(5 + 6 ) ; it</button>
</script> </body>
</body>
Cont..
➢ Using window.alert()
<body>
<script>
window.ale r t ( 5 + 6) ;
</script>
</body
Cont..
➢ window keyword.
< script >
alert(5 + 6);
</script>
➢ JavaScript Print
page</button>
JavaScriptDataTypes
➢ JavaScript variables can hold different data types:
* Multiplicati >>
on
** Exponentiat let z = 5 ** 2;
ion document.getElementById("demo").innerHTML = z;
(ES2016)
/ Division
% Modulus
(Remainder)
++ Increment
-- Decrement
Comparison Operators
Logical operator
Conditional and Looping Statements
• Control structure actually controls the
flow of execution of a program. Following
are the several control structure
supported by javascript.
✓ if … else
✓ switch case
✓ do while loop
✓ while loop
✓ for loop
if Statement
➢ IF statement is a conditional branching statement.
• Syntax : If statement
if(condition)
{
//Statement 1 ;
//Statement 2 ;
}
Example
<html>
<body>
<script type="text/javascript">
var num = prompt("Enter Number");
i f (num > 0 )
{
a l e r t ( " G i v e n number i s P o s i t i v e ! ! ! " ) ;
}
</script>
</body>
</html>
If – Else Statement
➢ If – Else is a two-way decision statement.
conditionally.
Syntax
i f (condition)
{
//Statement 1 ;
}
else i f ( c o n d i t i o n )
{
//Statement 2 ;
}
else
{
//Statement 3 ;
}
Switch Statement
➢ Switch is used to perform different actions on different
conditions.
different values.
Cont..
Syntax
switch(expression)
{
case c o n d itio n 1 :
//Statements;
break;
case c o n d itio n 2 :
/ /Stateme n ts;
break;
case c o n d itio n 3 :
/ /Stateme n ts;
break;
.
.
case c o n d itio n n :
/ /Stateme n ts;
break;
default:
//Sta te men t;
Example
<script type="text/javascript">
var day = prompt("Enter a number between 1 and 7 " ) ;
switch (day)
{
case ( d a y= " 1 " ) :
document . wr i t e( " Sunday" ) ;
break;
case ( d a y= " 2 " ) :
document.write("Monday");
break;
case ( d a y= " 3 " ) :
document.write("Tuesday");
break;
default:
document . writ e("I nvalid Weekday");
break;
}
</script>
For Loop
➢ For loop is a compact form of looping.
➢ 1. Loop Initialization
➢ 2. Test Condition
➢ 3. Iteration
Cont..
Syntax
//Statements;
}
While Loop
❖ While loop is an entry-controlled loop statement.
while (condition)
//Statements;
}
Do-While Loop
false.
Syntax
do
{
//Statements;
}
while(condition);
Cont.…
<script type ="text/javascript">
var i = 0 ;
do
{
document. write ( i +"<br>")
i++;
}
while ( i <= 5 )
</script>
Cont..
Part II
Loading
Thanks
?