Thursday, June 21, 2012

Java Script Tutorial 10 : JavaScript Popup Boxes

There are three kind of popup boxes in JavaScript
  • Alert box
  • Confirm box
  • 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 of Alert Box is as follows :

javascript alert box
Javascript alert box.
For example
javascript alert box in html
Javascript alert box in html.
Confirm Box :
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. Syntax of Confirm Box is as follows :

javascript confirm box
Javascript confirm box.
For example

javascript confirm box in html
Javascript confirm box in html.
Prompt Box :
A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null. Syntax of Prompt Box is as follows :

javascript prompt box
Javascript prompt box.
For example

javascript prompt box in html
Javascript prompt box in html.
To display line breaks inside a popup box, use a back-slash followed by the character n. For example

javascript alert box with line break
Javascript alert box.

Java Script Tutorial 9 : JavaScript Switch Statement


The switch statement are used to perform different action based on different conditions. The switch statement can be replaced by performing many if-then-else-if conditions, but when the answer of an evalution can give you many different answers, the switch-case statement can be more efficient. Syntax of switch statement is as follow :


switch(x)
{
case 1:
  execute code block 1
  break;
case 2:
  execute code block 2
  break;
default:
  code to be executed if x is different from case 1 and 2
}


The value of the variable x will be compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. For example


switch ( day )
{
case 0:
  y="Sunday";
  break;
case 1:
  y="Monday";
  break;
case 2:
  y="Tuesday";
  break;
}


If the value of variable day is 0 then result will be "Sunday", and result will be "Monday" and "Tuesday" if values will be 1 and 2 respectively.


We use the default keyword to specify what to do if there is no match. For example, there is no value equal to 0, 1 or 2, then write a default message, for example write "friday".


switch ( day )
{
case 0:
  y="Sunday";
  break;
case 1:
  y="Monday";
  break;
case 2:
  y="Tuesday";
  break;
default:
  y="Friday";
}


If there is no match found then result will be "friday".

Java Script Tutorial 8 : If Else Statements


Conditional statements are used to perform different actions based on different conditions. In JavaScript we have the following conditional statements.

  • if statement
  • if...else statement
  • if...else if....else statement
  • switch statement

If Statement :
We use the if statement to execute some code only if a specified condition is true. Syntax of if statement is as follow :


if (some condition)
  {
  code to be executed if condition is true
  }


For example


if (number<33)
  {
  x="failed";
  }


If value of variable number is less then 33 then result will be "failed".


If  .. else Statement :
We use the if  else statement to execute some code if a condition is true and another code if the condition is not true. Syntax for this is as follows :


if (some condition)
  {
       code to be executed if condition is true
  }
else
  {
       code to be executed if condition is not true
  }


For example


if ( number < 33 )
  {
  y="failed";
  }
else
  {
  y="passed";
  }


If the value of variable number less then 33 then result will be "failed", otherwise result will be "passed".


If .. else if  .. else Statement :
We use the if  else if  else statement to select one of several blocks of code to be executed. Syntax for this is as follow


if (condition 1 here)
  {
       code to be executed if condition1 is true
  }
else if (condition 2 here)
  {
       code to be executed if condition2 is true
  }
else
  {
       code to be executed if neither condition1 nor condition2 is true
  }


For example


if (number>90)
  {
  y="Passed : Excellent Result";
  }
else if (number<33)
  {
  y="failed";
  }
else
  {
   y="passed";
  }


If the value of variable number will be greater then 90 then result will be "Passed : Excellent Result". if value will be less then 33 then result will be "failed", otherwise (when value will be between 33 and 90 ) result will be "passed".

Wednesday, June 20, 2012

Java Script Tutorial 7 : Comparison and Logical Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values. Comparison operators can be used in conditional statements to compare values and take action depending on the result. There are many Comparison operators used in javascript, some of these are below :


Operator Symbol Uses/Description
= = is equal to
= = = is exactly equal to (value and type both)
! = is not equal
> is greater than
< is less than
> = is greater than or equal to
< = is less than or equal to


Logical operators are used to determine the logic between variables or values. We can use following logical operators in java script.


&& ( and  )
|| ( or )
! ( not )


conditional operator : JavaScript contains conditional operator that assigns a value to a variable based on some condition. Syntax of this conditional operator is as below.


somevariablename = (put condition here) ? value1 : value2


If condition meets then output 'll be 'value1', and if condition doesn't meet then output 'll be 'value2'.

Tuesday, June 12, 2012

Java Script Beginner Tutorial 6 : JavaScript Operators

JavaScript operators are used to perform operations on JavaScript variables. The assignment operator = is used to assign values to JavaScript variables. The arithmetic operator + is used to add values together. For example


x = 3;
y = 4;
z = x + y;


The output 'll be equal to 7. 


Arithmetic operators are used to perform arithmetic between variables and/or values. There are many arithmetic operators, we use in JavaScript, for example


+  ( used for addition),                -  ( used for subtraction ), 
*  ( used for multiplication ),        /  ( used for division ), 
%  ( used for modulus ),             ++  ( used for increment ) and 
--  ( used for decrement )


Assignment operators are used to assign values to JavaScript variables. These assignments operators can be used in JavaScript :


= ,  += ,  -= ,   *= ,   /=   and   %=


The + operator can also be used to add string variables or text values together. For example


str1 = "This is ";
str2 = "a fox";
str3 = str1 + str2;


The output 'll be "This is a fox."
If you add a number and a string, the result will be a string. For example 


x = "this is a string" + 10 ;


The output 'll be "this is a string10".