Tuesday, September 4, 2012

JavaScript Introduction


Introduction to Javascript

Introduction JavaScript


JavaScript is the number one scripting language on the internet,that  works in all most all  major browsers, such as Internet Explorer, Firefox, Chrome, Opera, and Safari.

What is JavaScript?

· JavaScript was designed to add interactivity to HTML pages

· JavaScript is a scripting language

· A scripting language is a lightweight programming language

· JavaScript is usually embedded directly into HTML pages

· JavaScript is an interpreted language (means that scripts execute without preliminary compilation)

· Everyone can use JavaScript without purchasing a license

 Many thinks that Java and Javascripts are relatives !!  But they  are two completely different languages in both concept  as well as design!
Java (developed by Sun Microsystems) is a powerful and much more complex programming language - in the same category as C and C++.
Example 1














Example 2




Example 3


 You can place an unlimited number of scripts in your document, and you can have scripts in both the body and the head section at the same time.
It is a common practice to put all functions in the head section, or at the bottom of the page. This way they are all in one place and do not interfere with page content.

Storing JavaScript in External Files


JavaScript can also be placed in external files.
External JavaScript files often contain code to be used on several different web pages.
External JavaScript files have the file extension .js.
Note: External script cannot contain the tags!
To use an external script, point to the .js file in the "src" attribute of the














JavaScript is highly Case Sensitive


Unlike HTML, JavaScript is case sensitive - therefore watch your capitalization closely when you write JavaScript statements, create or call variables, objects and functions.

Example 4

JavaScript Comments


Single line comments start with //.
Multi line comments start with /* and end with */.

Variables in JavaScript


  JavaScript variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like carname.

Rules for JavaScript variable names:


· Variable names are case sensitive (y and Y are two different variables)
· Variable names must begin with a letter or the underscore character.
Example

















The script above declares a variable,assigns a value to it, displays the value, changes the value,and displays the value again.



JavaScript Arithmetic Operators




JavaScript Assignment Operators



The Different uses of + Operator
The + operator can also be used to add string variables or text values together.
To add two or more string variables together, use the + operator.

a1="Have ";
a2=" a nice day";
a3=a1+at2;


Comparison Operators


Logical Operators


Conditional Operator

variablename=(condition)?value1:value2

Example

solute=(gender=="male")?"Dear Sir ":"Dear  Mam";


Conditional Statements

 JavaScript has  following conditional statements:
· if statement - Execute  a single or set of statements based on a specified condition is true
· if...else statement -This statement  executes 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
Syntax

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





If...else Statement
Syntax

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

Example















If...else if...else Statement
Syntax:

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


Example























The Switch Statement
 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
}


Example





JavaScript Popups

JavaScript has three  popup boxes: Alert box, Confirm box, and Prompt box.

Alert Box
An alert box is  a dialog box which is  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("something");

Example






 












Confirm Box


A confirm box is often used if you want the user to confirm or avoid 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
confirm("something");

Example



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
prompt("sometext","defaultvalue");

Example






0 comments:

Post a Comment