The most popular scripting language is Java script language on internet which works on almost all browsers like internet explorer, Google chrome, Firefox, opera and safari. For learning JavaScript we need to know html or xhtml technology. Java script helps in making the html webpage interactive. It is a scripting language so the programs of it are executed on server side. Java script is a open source like html and php. It’s not license reserved. Java and java script are not at all equal.
The html programs can be read and written by java script. Java script inserts dynamic script into the html webpage to interact. JavaScript is used in validating data, detecting the browsers. To insert the java script into html program, script tag is used. The JavaScript will write between script tag only.
Syntax of script tag:
-----
Statements
……
The script tag will be written in the body and head of the html program.
……
Statements
……
The browser which not supports the JavaScript, that browser displays the program as page content. If there is
element in the program then the JavaScript program should be below of the
element.
We need to write java script program in such a way when a user clicks the button then a event should occur in such case the script should be written inside the function. Actually events will be used with functions. We can place the script in head and body at the same time.
External java script should not contain tag. The set of statement to be executed at browser is known as java script. Java script is a case sensitive. To command the browser JavaScript language is used.
Document .write (“text”);
The above statement is used to print the text.
JavaScript comments:
Comments are used for the readability of the code.
There are two types:
- Single line comment
- Multi line comment
- Single line comment:
The comment line in single line.
Syntax:
//statement
- Multi line comment
The comment line for block of statement that is multi line
/*
Statements
*/
The variables in the JavaScript:
Variable in java script are case sensitive.
Variables should begin with letter or underscore.
The variable which is declared within the function is known as local variables. The local variables will be accessed within the function. The local variables will be destroyed when the function gets excited.
The variables which are declared outside the function is known as global variables. These variables can be access throughout the webpage. Global variables get excited when the page gets closed. The variables without using var then it are Global variable. The if a value assigned to a variable without declaration then it is automatically known global variable.
Operators:
To assign the value we use =
To add values we use +
To multiply we use *
To divide we use /
To modulus we use %
For increment ++
For decrement –-
+operator can be used to add string or text values.
The comparison operators:
== equal to
!= not equal to
- Greater than
< Less than
>= greater than and equal to
<=less than and equal to
Logical operators:
&& - And
|| - or
! - Not
Conditional operator:
Variable based on condition.
Syntax:
Variable name= (conditional operator)? value1:value2;
Conditional statement:
Conditional statement where we often write code.
The conditional statements are:
1. If statement
2. If else statement
3. If else if else if statement
4. Switch statement
1. If else statement syntax:
If(condition)
{
Code or statement
}
2. If else statement syntax:
If(condition)
{
Code the condtion is true
}
Else
{
Code the condtion is false
}
3. If else if else if statement syntax:
If(condition1)
{
Code to execute
}
Else if(condition2)
{
Code to execute
}
Else
{
Code execute condition 1 and condition2 are not true
}
4. Switch statement syntax:
Switch (a)
{
Case 1:
Code 1execute
Break;
Case 2:
Code2 execute
Break;
Default:
Code execute if a is different from case1 and case 2
}
There are three kinds of popup Boxes:
- Alert box
- Confirm box
- Prompt box
1.Alert box:
If the information comes through the user when the alert box is popup, so that the ok will be clicked by the user.
Syntax:
Alert (“text”);
2. Confirm box:
The information to accept or verify by the user, confirm box is used. When the confirm box appears there will be 2 options for the user that is ok and cancel. To return the box to true, the user should click ok and to return the box to false, the user should click cancel.
Syntax:
Confirm (“text”);
3. Prompt box:
To input a value into the page before entering, then the prompt box is used. when the prompt box pops then the using will be having 2 option that is ok and cancel to proceed after entering input value. If ok is clicked then the box returns to input value and if cancel is clicked then the box returns to null value.
Syntax:
Prompt (“text”, “default value”);
JavaScript functions:
A function gets executed by event or call by function.
We can call function from anywhere in the page. We can define a function in head and body tags.
Syntax:
Function function name (var1, var2………..varn)
Return statement:
Return statement is used to specify which is going to be returned the function. The function which returns a value should use return statement.
Java script loops:
Loops are executed with block of code or statements in specified times and when the condition becomes true.
There are 2 loops in JavaScript:
1. For loop and 2. While loop
1. For loop:
This loop is used to execute the block of code in specified number of times. To run the script in specified number times for loop is used.
Syntax:
For (variable = star value; variable<=end value; variable=variable increment)
{
Code
}
2. While loop:
The loop that executed until the specified condition is true.
Syntax;
While (condition)
{
Code
}
Do while:
The loop executes once and executes until the specified condition is true.
Syntax:
Do
{
Code
}
While (variable<=end value)
Break statement:
The break statement will break the loop and executes next following code after the loop.
Continue statement:
The continue statement will continue the loop and executes next value.
For in statement:
The for in statement loop through the code in the properties of the object.
Syntax:
For(variable in object)
{
Code
}
The code in the form in statement will execute once for each property.
Events:
The actions that can be detected by the JavaScript are known as events.
By using JavaScript we can create dynamic WebPages.
Every element on the webpage will have an event that can trigger JavaScript.
Events are used with the combination of functions. Events cannot be executed before the function occurs.
Onload and on unload events:
When the user enters and leaves the webpage then the events on load and onunload will be triggered.
Onload is used to check the browser, browser version and loading property of the webpage which the user is using.
Onload and onunload events are used to set the cookieswhile leaving the webpages.
Onfocus, on blur and on change events:
These events are used with the combination of validation in the fields of form.
Syntax:
On submit:
Before submitting, this event will be used for validating all form fields.
Syntax:
Onmouseover:
When a user mouse over an html element then onmouseover is used to trigger the function.
Try catch statement:
To test a block of code to test we use try catch statement.
Syntax:
Try
{
Code
}
Catch (error)
{
Error code
}
The try and catch keywords should be in lowercase letters. If we use uppercase letters then they will be errors in JavaScript.
The throw statement:
The throw statement will be sued with try and catch to create exception.
Syntax:
throw(exception)
The exception may be object, Boolean, integer, string.
Here throw should be in lowercase. If it is uppercase then it will be error.