Java script
Java script
- Java script is a scripting language that is used to create the dynamic web page
- Dynamic web page is the web page where we can update the contents
Steps :
Call / implement the function inside the web page over the html control
Note : There are various event handlers are used with Html Control to handle java
script function/code. most common handler is 'onClick' that handle the click
event.
<head>
<script type="text/javascript">
.....
.....
..
..
</script>
</head>
<body>
............
.....
</body>
calling of function :
like :
Common Rules :
Java script is a case sensitive language, use the lower case to write code or
like
Note : to print/write something on web page , java script provides the method :-
document.write("....");
Here 'document' is a pre-define object of javascript and 'write' is the method associated
to that object.
there may be multiple methods that can be associated with a particular object.
javascript provides various of objects and their associated methods which we learn in
upcomming classes.
we can also write any html control using document.write() method, remember that all the
tags are enclosed inside "" and all the quotation("") inside the tag are replaced by ' '.
Example :
java1.html
java2.html
java3.html
-------------------------------------------------------------------------------------------------------------
Objects
Events
Data Types
Variable
Operators
Control Statements
Array/String/Date/...
-------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
- each object provides some functions to perform specific action/task related to particular
category.
- objects makes the working easy and fast.
1. document
2. form
3. frame
4. history
5. location
6. navigator
7. window
document : it is used to refer current web page's body. we can access the html elements
Images etc.
write()
writeln()
getElementById()
getElementByName()
some properties :
selection
title
vlinkColor
URL
ex: document.html
example
1. <script type="text/javascript">
2. function getcube(){
3. var number=document.getElementById("number").value;
4. alert(number*number*number);
5. }
6. </script>
7. <form>
10. </form>
----------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
- to do so, we should use the 'id' or 'name' property with html elements
getElementById("id") or getElementByName("name")
- to insert text inside paragraph, div, span ,.. we use 'innerHTML' property
- we can also set/change the css properties of elements using java script
like this :
b="ram";
c=12.33;
java script provides the variant type variable (no type is specified)
integer
float
character
boolean
ex:
document.html
document1.html
document2.html
ex2:-
<html>
<head>
<title>document 1 </title>
<script type="text/javascript">
function start() {
document.getElementById("m1").scrollAmount="3";
}
function stop(){
document.getElementById("m1").scrollAmount="0";
}
</script>
</head>
<body>
<center>
<fieldset style="width:300px; height:400px; border-width:5px; border-style:double;">
<legend> Latest Updates </legend>
<span align="right">
<a href="#" onClick="stop();"> stop scroll</a>
<a href="#" onClick="start();"> start scroll</a>
</span>
</body>
</html>
-------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
- alert()
- prompt()
- confirm()
alert() :
syntax:
alert("message");
or
alert("message" + variable);
ex:
alert.html
prompt() :
ex:
var nm;
ex 2:
var a,b,c;
a=parseInt(a);
b=parseInt(b);
c=a+b
ex: prompt.html
confirm() : - It shows the Confirmation box with two options : yes and no (ok / cancel)
ex: confirm.html
<html>
<head>
<title>document 1 </title>
<script type="text/javascript">
var a,b,c;
function getInput(){
a=prompt("Enter first no ");
b=prompt("Enter Second no ");
a=parseInt(a);
b=parseInt(b);
}
function getSum() {
c=a+b;
alert("Sum is "+ c);
}
function exit() {
if(confirm("Do you want to close Browser ?")){
window.close();
}
</script>
</head>
<body>
<center>
<fieldset style="width:300px; height:200px; border-width:5px; border-style:double;">
<legend> Simple Arithmetic </legend>
<tr>
<td> <input type="button" value="Input" onClick="getInput();"/> </td>
<td> <input type="button" value="Sum" onClick="getSum();"/> </td>
</tr>
<tr><td colspan="2"><center> <input type="button" value="Close Browser " onClick="exit();"/> </td>
</table>
</fieldset>
</body>
</html>
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
- Events are the Actions applied on control by user to make intraction with web
page.
MouseEvents are :
Click
DblClick
MouseMove
MouseOver
MouseOut
MouseDrag
MouseUp
MouseDown
....
....
KeyboardEvents :
KeyUp
KeyDown
KeyPressed
KeyTyped
.....
EventHandlers:
They are the attributes used with html control to handle specific type of event.
they start with 'on' and followed by Event Name like - onClick.
onBlur : occurs when an element loses the input focus(cursor goes outside
of textbox)
- It is used to upload any Image or other files and submit to the server
Examples :
Event1.html
Event2.html
Event3.html
Event4.html
Event5.html
<!--
comments
-->
// comments
........
........
*/
ex:-
Event2.html
-----------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
Element.setAttribute("attribute","value");
Element.js-property = value;
like :
var a = document.getElementById("txtno1");
a.value=100;
or
a.setAttribute("value",100);
Attribute1.html
//Attribute2.html
DOM.html
-------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
syntax:
ELEMENT.setAttribute("style","property set");
or
ELEMENT.style.JsCssPropertySet = value;
Note :
When we use javascript to set Css Property, all the css property is written as camel case
like :-
background-color : backgroundColor
text-decoration : textDecoration
font-style : fontStyle
text-transform : textTransform
list-style-type : listStyleType
like :
var dv = document.getElementById("div1");
dv.style.backgroundColor="red";
or
dv.setAttribute("style","background-color:red");
dv.style.textTransform="uppercase";
dv.style.fontStyle="italic";
OR
dv.setAttribute("style","text-transform:uppercase;font-style:italic");
Attribute3.html
Attribute4.html
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
..
..
..
..
..
..
..
..
-----------------------------------------------------------------------------------------------------------
Operators :
------------------------------------------------------------------------------------------------------------
Arithemetical Operator
Logical Operator
Assignment Operator
-------------------------------------------------------------------------------------------------------------
ArithMetical Operators :
ex:
ex :Operators.html
-----------------------------------------------------------------------------------------------------------
Relational Operator :
ex:
-----------------------------------------------------------------------------------------------------------
Logical Operators :
ex:
|| (Logical Or)
! (Logical Not)
-------------------------------------------------------------------------------------------------------------
ex: =
------------------------------------------------------------------------------------------------------------
- do the arithmetical operations with variable and store the result back to same
variable.
ex:
+= -= /= *= %=
ex:
var a=10;
------------------------------------------------------------------------------------------------------------
var a=10;
var a=10;
-------------------------------------------------------------------------------------------------------------
Conditional Operator :
?:
Used to check condition and then do any of the two expression depending on condition
syntax:
ex:
[operator2.html]
-------------------------------------------------------------------------------------------------------------
-------> Branching
| |
| |
| -------> if statement
-------> Looping
| |
| |
| -------> do statement
-----> break
-----> continue
-----> goto
------> return
Branching :
statements:
a. simple if
b. if else
c. multiple if
d. nested if
switch statment
conditional operator
Simple if :
syntax:
if (<condition>) {
.......
.......
[ex: absolute.html]
if else Statement :
is excuted.
syntax:
if (<condition>) {
....
....
else {
.....
.....
Note : if any block has only one statement,we can skip the use of {}.
Multiple if :
only one block get executed at a time that condition becomes true first.
if (<condition>) {
....
else if(<condition>){
....
}
else if(<condition>) {
.....
else {
.....
[Division.html]
Nested If :
if (<condition>) {
if(<condtion>) {
...
else {
.....
ex : input any number in textbox and show the square if and only if the number is positive
as well as even.
[nestedIf1.html]
input the age of person and select the gender (from radio button). show the eligibility for
[nestedif2.html]
Note : you can also use the logical operator - && in case of nesting.
Switch Statement :
- Used to define the set of values to perform the particular type of action
- We can define the different actions over different values or same action over
different values.
- The source variable is written inside switch() and the values for comparision
Syntax:
switch(<variable>) {
case <value1> :
.......
break;
case <value2> :
...........
break;
case <value3> :
.............
break;
..
..
default :
.....
ex : Select the week day sequence from combobox (1 - 7) and show the name of the
realated day.
[dayExample.html]
Input two numbers in a text box and select the operations from combobox(Add,Sub.,Multi.,Div.) and do
the related operation.
[ChoiceOp.html]
Conditional Operator :
?:
Used to check condition and then do any of the two expression depending on condition
syntax:
ex:
[operator2.html]
------------------------------------------------------------------------------------------------------------
Looping Statements :
Test Condition
Counter Updation
Statment Block
var i;
i=1;
updation ---------> increase or decrease the value after each repeate , like
i++ i--
or
i=i+1 i=i-1
or
i+=1 i-=1
while(i<=10)
statement block -------> the statement(s) which are executed during the looping, like
...
-----------------------------------------------------------------------------------------------------------
do statement : It excuted the statement first then check the condition, so it is an exit control loop
syntax:
<set counter>
do {
........
......... <statements>
......... <updation>
} while(<condition>);
ex: show the numbers from 1 to 10 when user run/open the browser.
function show() {
do {
while Statement :
- Each time it check condition first then repeate the loop until the condition
become false
Syntax:
<set counter>
while (<condition>) {
.....
..... //statements
......//updation
ex:
function show() {
var i=20;
while (i>=1) {
document.write("<br/>"+i);
i--;
...
...
<body onLoad="show();">
...
</body>
Q. Input a number to textbox and show the table of number when user click on button.
...
function table() {
var n,i,t;
n=document.getElementById("txt1").value;
i=1;
while(i<=10) {
t=n*i;
i++;
...
...
<body>
.......
</body>
for loop :
Syntax:
....
function showNum() {
var i;
for(i=1;i<=10;i++)
document.write("<br/>"+i);
.....
<body onLoad="showNum();">
...
</body>
see :
loop1.html
loop2.html
loop3.html
loop4.html
loop5.html
-------------------------------------------------------------------------------------------------------------
jump statements :
used to shift the control of program from one place to another place.
they are :-
1. break : It is used to terminate the current loop body and jump out side of block.
ex:
for (i=1;i<=100;i++) {
if (i>=10)
break;
document.write("<br/>"+i);
output :
..
2. continue :
for(i=1;i<=100;i++ {
continue;
document.write("<br/>"+i);
output:
..
10
90
91
...
100
goto :
- label can be created any where inside the function followed by color (:)
ex:
function show() {
var i=1;
print :
document.write("<br/>" + i);
i++;
output :
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
String :
- It is a set of characters
- we can create object of String class then use the various String methods also.
creating Object :
or
like
or
String Functions : -
toUpperCase()
toLowerCase()
substring()
length()
contains()
startsWith()
endsWith()
indexOf()
charAt()
equals()
toUpperCase() and toLowerCase() : retuns the uppercase and lower case of the string
like:
function show() {
nm = new String();
nm= document.getElementById("txtnm").value;
..
like :
nm = new String("Ramesh");
alert(nm2); // output : me
ex:
// or Nm = document.getElementById("txtnm").value;
var n= Nm.length();
like :
var n = str.indexOf("e");
like :
Nm = new String("Amriesh");
var ch = Nm.charAt(3);
ex:
if (Nm.startsWith("Mr.")
document.write("Welcome Sir");
else if(Nm.startsWith("Mrs")
document.write("Welcome Madam");
else
document.write("Welcome");
like
nm1=new String("ramesh");
nm2=new String("Ramesh");
if(nm1.equals(nm2)) // if(nm1==nm2)
else
__________________________________________________________________
Array :
syntax :
like :
a = new Array(5);
like :
A = new Array(5);
A[0]=10;
A[1]=100;
A[2]=30;
A[3]=50;
A[4]=200;
we can also use loop to access the values of array like this :
example :
Enter 5 names in an array and show all the names in web page.
[array.html]
------------------------------------------------------------------------------------------------------------
creating function:
part of function==>
1. function declaration
2. function definition
3. function calling
= Function makes the code/statements reusable. means we define the function once but we
can call them many time.
syntax:
function <name>(...)
....
like :
function show() {
....
}
types of functions:
2. Parametrized : That accept the argument and user can pass the value
at the time of function call.
like :
function sum(x,y)
var z= x+y;
document.write("sum is "+z);
..
<body onLoad="sum(2,40);">
</body>
...
return(<value/expression>);
example:
<script....>
function sum(x,y,z) {
return(x+y+z);
function average(x,y,z) {
document.write("average : "+av);
..
<body onLoad="average(20,40,80);">
</body>
-------------------------------------------------------------------------------------------------------------