August 12th, 2009 by soujanya
A function will be executed by an event or by a call to the function.
JavaScript Functions
To keep the browser from executing a script when the page loads, you can put your script into a function.
A function contains code that will be executed by an event or by a call to the function.
You [...]
«« Read More
August 12th, 2009 by soujanya
JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.
Alert Box
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click “OK” to proceed.
Syntax
alert(“sometext”);
Example
<html>
<head>
<script type=”text/javascript”>
function show_alert()
{
alert(“I am an alert [...]
«« Read More
August 12th, 2009 by soujanya
Conditional statements are used to perform different actions based on different conditions.
The JavaScript Switch Statement
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
This [...]
«« Read More
August 12th, 2009 by soujanya
Comparison and Logical operators are used to test for true or false.
Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x=5, the table below explains the comparison operators:
Operator
Description
Example
==
is equal to
x==8 is false
===
is exactly equal to (value and type)
x===5 is true
x===”5″ is false
!=
is not equal
x!=8 [...]
«« Read More
August 6th, 2009 by soujanya
Loops execute a block of code a specified number of times, or while a specified condition is true.
JavaScript Loops
Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use [...]
«« Read More
August 6th, 2009 by soujanya
Loops execute a block of code a specified number of times, or while a specified condition is true.
The while Loop
The while loop loops through a block of code while a specified condition is true.
Syntax
while (var<=endvalue)
{
code to be executed
}
Note: The <= could be any comparing statement.
Example
The example below defines a loop that starts [...]
«« Read More
August 6th, 2009 by soujanya
The break Statement
The break statement will break the loop and continue executing the code that follows after the loop (if any).
Example
<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.
Example
<html>
<body>
<script type=”text/javascript”>
var i=0
for (i=0;i<=10;i++)
{
if (i==3)
{
continue;
}
document.write(“The number [...]
«« Read More
August 6th, 2009 by soujanya
JavaScript For…In Statement
The for…in statement loops through the elements of an array or through the properties of an object.
Syntax
for (variable in object)
{
code to be executed
}
Note: The code in the body of the for…in loop is executed once for each element/property.
Note: The variable argument can be a named variable, an array element, [...]
«« Read More
August 6th, 2009 by soujanya
Events are actions that can be detected by JavaScript.
Events
By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be detected by JavaScript.
Every element on a web page has certain events which can trigger a JavaScript. For example, we can use the onClick event of a button [...]
«« Read More
August 6th, 2009 by soujanya
The try…catch statement allows you to test a block of code for errors.
JavaScript – Catching Errors
When browsing Web pages on the internet, we all have seen a JavaScript alert box telling us there is a runtime error and asking “Do you wish to debug?”. Error message like this may be useful for [...]
«« Read More