CH 4
CH 4
CH 4
Java Script
By Tesfahun
Outline
Introduction to JavaScript
JavaScript Basic
JavaScript Syntax
Browser/page manipulation
to websites
Why use JavaScript?
To create dynamic and interactive Web pages
Perform calculations
How Does It Work?
Executes on client machine
Fast, no connection needed once loaded
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>
<title>My First JavaScript code!!!</title>
<script type="text/javascript">
alert("Hello World!");
</script>
</head>
<body>
</body>
</html>
Cont..
Example 2
<script>
function sum(a,b)
return a + b;
alert(total);
</script>
Adding JavaScript to HTML Pages
1. Embedding code
2. Inline code
3. External file
Cont..
I. Embedding code:-
<html>
<head>
<title> page title</title>
<script>
document.write("Welcome to Javatpoint");
</script>
</head>
<body>
…..
</body>
</html>
Cont..
2. inline code:-
external JavaScript
sample.js
function sample() {
alert('Hello from sample.js!')
}
JavaScript Comments
JavaScript Output
document.getElementById(id) method.
document.write(5 + 6); it</button>
</script> </body>
</body>
Cont..
Using window.alert()
<body>
<script>
window.alert(5 + 6);
</script>
</body
Cont..
window keyword.
<script>
alert(5 + 6);
</script>
JavaScript Print
<button onclick="window.print()">Print this
page</button>
JavaScript Data Types
JavaScript variables can hold different data types:
let x = {firstName:"John",lastName:"Doe"}; //
Object
JavaScript Data Types and Variables
* Multiplicati >>
on
** Exponentiat let z = 5 ** 2;
ion (ES2016 document.getElementById("demo").innerHTML = z;
)
/ Division
% Modulus
(Remainder)
++ Increment
-- Decrement
Comparison Operators
Logical operator
Cont..
Part II
Loading
Conditional and Looping Statements
switch case
do while loop
while loop
for loop
if Statement
IF statement is a conditional branching statement.
conditionally.
Syntax
if (condition)
{
//Statement 1;
}
else if(condition)
{
//Statement 2;
}
else
{
//Statement 3;
}
Switch Statement
Switch is used to perform different actions on different
conditions.
different values.
Cont..
Syntax
switch(expression)
{
case condition 1:
//Statements;
break;
case condition 2:
//Statements;
break;
case condition 3:
//Statements;
break;
.
.
case condition n:
//Statements;
break;
default:
//Statement;
}
Example
<script type="text/javascript">
var day = prompt("Enter a number between 1 and 7");
switch (day)
{
case (day="1"):
document.write("Sunday");
break;
case (day="2"):
document.write("Monday");
break;
case (day="3"):
document.write("Tuesday");
break;
default:
document.write("Invalid 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>
Array in JavaScript
What is array ?
An array is a special variable, which can hold more than one
value:
1. By array literal
var arrayname=[value1,value2.....valueN];
<script>
var name=[“Aman",“Helen",“Meklit","Tesfahun"];
for (i=0;i<name.length;i++)
{
document.write(name[i] + "<br/>");
}
</script>
JavaScript Array directly (new keyword)
The syntax of creating array directly is given below:
var arrayname=new Array();
var temp=new Array("Jai","Vijay","Smith");
for (i=0;i< temp.length;i++){
document.write(temp[i] + "<br>");
}
</script>
Java script function
A function is a block of code that performs a specific
task.
function functionName(parameters)
// function body
}
Calling a function
To use a function, you need to call it.
functionName(arguments);
window object.
<h2>JavaScript Window</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
window.innerWidth + "px<br>" +
window.innerHeight + "px";
</script>
</body>
Cont..
Some other methods
window.open() - open a new window
window.open(url, windowName,[windowFeatures]);
window.moveTo() - move the current window
window.moveTo(x, y);
window.resizeTo() - resize the current window
window.resizeTo(width,height);
Cont..
Window Location Href The window.location.href property
returns the URL of the current page
<body>
<h3>The window.location object</h3>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The full URL of this page is:<br>" +
window.location.href;
</script>
</body>
Output
HTML Forms and JavaScript
•
Form validation
JavaScript provides a way to validate form's data
on the client's computer before sending it to the
web server
Form validation generally performs two functions.
• Basic Validation
checked to make sure all the mandatory fields are filled
in.
It would require just a loop through each field in the
form and check for data.
• Data Format Validation
• the data that is entered must be checked for correct
form and value.
• Your code must include appropriate logic to test
correctness of data.
Cont..
cont…
Thanks
?