Web Technology Unit 4
Web Technology Unit 4
if statement - use this statement to execute some code only if a specified condition is true.
if...else statement - use this statement to execute some code if the condition is true and
another code if the condition is false.
if...else if....else statement - use this statement to select one of many blocks of code to be
executed.
switch statement - use this statement to select one of many blocks of code to be executed
If Statement:
Use the if statement to execute some code only if a specified condition is true.
Syntax:
if (condition)
If...else Statement:
Use the if....else statement to execute some code if a condition is true and another code if the condition
is not true.
Syntax:
if (condition)
else
}
Example:
<script type="text/javascript">
document.write("Good morning!");
else
document.write("Good day!");
</script>
Use the switch statement to select one of many blocks of code to be executed.
Syntax:
switch(n)
case 1:
break;
case 2:
break;
default:
code to be executed if n is different from case 1 and 2
2) JavaScript Functions :
A function contains code that will be executed by an event or by a call to the function.
User can call a function from anywhere within a page (or even from other pages if the function is
embedded in an external .js file).
Syntax:
function functionname(var1,var2,...,varX)
some code
The parameters var1, var2, etc. are variables or values passed into the function. The { and the } defines
the start and end of the function.
Example:
<html>
<head>
<script type="text/javascript">
function displaymessage()
alert("Hello World!");
</script>
</head>
<body>
<form>
</body>
</html>
3) JavaScript Loops:
The same block of code to run over and over again in a row.
It is Used to repeatedly run a set of instruction while a given condition is true or false.
In JavaScript, there are two different kind of loops:
1. for - loops through a block of code a specified number of times
2. while - loops through a block of code while a specified condition is true
The for loop is used when you know in advance how many times the script should run.
Syntax:
for (variable=startvalue;variable<=endvalue;variable=variable+increment)
code to be executed
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less
than, or equal to 5. i will increase by 1 each time the loop runs.
Example:
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{
document.write("<br />");
</script>
</body>
</html>
Loops execute a block of code a specified number of times, or while a specified condition is true.
The while loop, loops through a block of code while a specified condition is true.
Syntax:
while (variable<=endvalue)
code to be executed
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less
than, or equal to 5. i will increase by 1 each time the loop runs:
Example:
<html>
<body>
<script type="text/javascript">
var i=0;
while (i<=5)
document.write("<br />");
i++;
</script>
</body>
</html>
Syntax:
do
code to be executed
while (variable<=endvalue);
Example:
<html>
<body>
<script type="text/javascript">
var i=0;
do
document.write("<br />");
i++;
}
while (i<=5);
</script>
</body>
</html>
4) Window Object :
Window object is a top-level object in Client-Side JavaScript.
Window object represents the browser's window.
It represents an open window in a browser.
The document object is a property of the window object. ...
All global variables are properties and functions are methods of the window object.
The following table lists important properties of Window Object.
JavaScript Example:
function validateForm()
var x = document.forms["myForm"]["fname"].value;
if (x == "")
return false;
</form>