Archive for the ‘Java Script’ Category

JavaScript Functions

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 [...]

Popup Boxes in JavaScript

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 [...]

JavaScript Switch Statement

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 [...]

Comparison and Logical Operators in JavaScript

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 [...]

For Loop in JavaScript

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 [...]

While Loop in JavaScript

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 [...]

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).

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 [...]

For…In Statement in JavaScript

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, [...]

JavaScript Events

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 [...]

Try…Catch Statement in JavaScript

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 [...]