Java Script
Java Script
➢HTML
➢CSS
What you should know
Bonjour
French
JavaScript: Introduction
Aloha
kakahiaka
Hawaiian
What is scripting language?
お早うございま
す
JS: Client-side language
ohayō
gozaimasu
Japanese
What is javascript?
➢JavaScript was designed to add interactivity to
HTML pages
안녕하십니까
Issues with a client-side language
annyeong
hashimnikka
Korean
A bit History
Maayong
Buntag
Cebuano
A bit History
Maayong
Buntag
Cebuano
What do you need to write JS?
function show_alert()
{
alert("I am an alert box!");
}
</script>
</head>
<body>
<input type="button" onclick="show_alert()" value="Show alert box" />
</body>
JavaScript Popup Boxes (Confirm)
• 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.
JavaScript Popup Boxes (Confirm)
<html>
<body>
<p>Sample for confirm box</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
if (confirm("Press a button!") == true) {
alert("You pressed OK!");
} else {
alert("You pressed Cancel!");
}
}
</script>
</body>
</html>
sample1.html
Note:
If you enter an email address without the @,
you'll get an alert asking you to re-enter the data.
What is:x.indexOf(@)==-1? This is a method that
JavaScript can search every character within a string
and look for what we want. If it finds it will return the
position of the char within the string.
If it doesn't, it will return -1. Therefore,
x.indexOf("@")==-1 basically means: "if the string
doesn't include @
sample2.html
Note:
if(document.login.userName.value==""). This
means "If the box named userName of the form named
login contains nothing, then...". return false. This is
used to stop the form from submitting. By default, a
form will return true if submitting. return
validate() That means, "if submitting, then call
the function validate()".
JavaScript Prompt (User Input)
<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>
The return Statement
• The return statement is used to specify the
value that is returned from the function.
• So, functions that are going to return a value
must use the return statement.
• The example below returns the product of
two numbers (a and b):
The return Statement
<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3));
</script>
</body>
</html>
The Lifetime of JavaScript Variables
• If you declare a variable within a function, the
variable can only be accessed within that function.
When you exit the function, the variable is
destroyed. These variables are called local variables.
You can have local variables with the same name in
different functions, because each is recognized only
by the function in which it is declared.
Syntax
while (var<=endvalue)
{
code to be executed
}
While Loop Example
<html>
<body>
<script type="text/javascript">
var i=0;
while (i<=5)
{
document.write("The number is " + i);
document.write("<br />");
i++;
}
</script>
</body>
</html>
The do...while Loop
• The do...while loop is a variant of the while loop. This
loop will execute the block of code ONCE, and then it
will repeat the loop as long as the specified condition
is true.
Syntax
do
{
code to be executed
}
while (var<=endvalue);
The do...while Loop
<html>
<body>
<script type="text/javascript">
var i=0;
do
{
document.write("The number is " + i);
document.write("<br />");
i++;
}
while (i<=5);
</script>
</body>
</html>
JavaScript Break and Continue Statements
The break Statement
The break statement will break the loop and continue
executing the code that follows after the loop (if any).
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=10;i++)
{
if (i==3)
{
break;
}
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
The continue Statement
• The continue statement will break the current loop and continue with the
next value. The break statement will break the loop and continue
executing the code that follows after the loop (if any).
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=10;i++)
{
if (i==3)
{
continue;
}
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>