Java Script:: Body of The Code
Java Script:: Body of The Code
Java Script:: Body of The Code
HTML pages which we have discussed so far are considered as static HTML pages,these pages
layout remains same. But , if you want to create the dynamic page , with the programming
support then you have to make use of the scripting languages.
Scripting languages are not the complete programming laguages then work within the HTML
code.
The Scripting code is written in the <script> tag , and the general form is ,
<script langauge="JavaScript/VBScript">
Body of the code
</script>
(a) document.write( )
The document object is used to identify the webpage and it provides the method or
the function known as write( ) to display the infomation on the browser windows.
<html>
<head>
<title>Using the JavaScript </title>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
document.write("Welcome to JavaScript");
</script>
</body>
</html>
output :
(b) alert( )
This function will display a dialog box on the screen ,via, which we can display
the information on the screen.
alert("message");
<html>
<head>
<title>Using the JavaScript </title>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
alert("Welcome to JavaScript");
</script>
</body>
</html>
output :
Note that the var type variable is capable of holding any type of value.
prompt( ) :
This function is used to read the information from the
user.
prompt("message");
<html>
<head>
<title>Using JavaScript</title>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
var sname;
output :
Note : Whatever you enter or input using the prompt( )
will be considered as a string.
parseInt( ) :
This function is used to convert the string into integer.
<html>
<head>
<title>Using JavaScript</title>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
var a,b,c;
c=a+b;
output :
Now, consider the following program,
<html>
<head>
<title>Using JavaScript</title>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
var a,b,c;
a=parseInt(a);
b=parseInt(b);
c=a+b;
if(condition)
statement1;
else
statement2;
where,statement1 will get executed when the condition is true and the statment2 will
get executed when the condition is false.
<html>
<head>
<title>Using JavaScript</title>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
var num;
num=parseInt(num);
if(num%2==0)
document.write("number is even");
else
document.write("number is odd");
</script>
</body>
</html>
output :
Q Write a javascript code to find the largest of three numbers.
<html>
<head>
<title>Using JavaScript</title>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
var a,b,c;
a=parseInt(a);
b=parseInt(b);
c=parseInt(c);
if(a>b)
if(a>c)
document.write("a is largest");
else
document.write("c is largest");
else
if(b>c)
document.write("b is largest");
else
document.write("c is largest");
</script>
</body>
</html>
Wednesday, July 12, 2006
Loops :
The term Loops refers to executing a statement or a group of statement till the specified
condition is true.
<html>
<head>
<title>Using JavaScript</title>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
var i,j,n;
document.write("<br><br><br>") ;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
document.write(j);
}
document.write("<br>");
}
</script>
</body>
</html>
output :
document.write("<br><br><br>") ;
rev=0;
while(num!=0)
{
rev=rev*10+num%10;
num=parseInt(num/10);
}
document.write("Reverse :"+rev);
</script>
</body>
</html>
output :
(iii) do...while loop
do
{
/*body of the loop*/
}while(condition);
<html>
<head>
<title>Using JavaScript</title>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
var num,sum,org;
num=parseInt(num);
org=num;
document.write("<br><br><br>") ;
sum=0;
do
{
sum=sum+(num%10)*(num%10)*(num%10);
num=parseInt(num/10);
}while(num!=0);
if(sum==org)
document.write("Armstrong Number .");
else
document.write("Not an armstrong number.");
</script>
</body>
</html>
output :
<html>
<head>
<title>Using JavaScript</title>
<script language="JavaScript">
function factorial(num)
{
var fact,i;
fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}
</script>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
var num,fact;
document.write("<br><br><br>") ;
fact=factorial(num);
output :
(ii) When the function will not return a value
<html>
<head>
<title>Using JavaScript</title>
<script language="JavaScript">
function factorial(num)
{
var fact,i;
fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
}
</script>
</head>
<body bgcolor="#ccffee">
<script language="JavaScript">
var num;
num=prompt("Enter the number:");
document.write("<br><br><br>") ;
factorial(num);
</script>
</body>
</html>
output :
Events as we know are the actions performed by the computer system or the user.
(i) onLoad( ) :
(ii) onClick( ) :
(iii) onDblClick( ):
(iv) onFocus( ) :
(v) onBlur( ) :
This will occur when the control will lost the focus.
(vi) onMouseOver( )
(vii) onMouseOut( )
etc....
Question : Design the JavaScript code to display the dialog box when the
page is loaded in the memory.
<html>
<head>
<title>Using Event Handling</title>
<script language="JavaScript">
function wel( )
{
alert("Welcome to JavaScript Event Handling");
}
</script>
</head>
<body onLoad="wel( )" bgcolor="red">
</body>
</html>
output :
Q Design the JavaScript code to create the button and when the user click on it a message is
displayed.
<html>
<head>
<title>Using Event Handling</title>
<script language="JavaScript">
function msg( )
{
alert("You Clicked on Me.........");
}
</script>
</head>
<body bgcolor="red">
<br>
<br>
<br>
<center>
<form name="myform">
<input type=button name="cmdbutton" value="Click Me" onClick="msg( )">
</form>
</center>
</body>
</html>
output :
Q Design the JavaScript code , for creating the application which containts , three buttons,
Red , Green and Blue to change the background color
accordingly.
<html>
<head>
<title>Using the Event Handling in JavaScript.</title>
<script language="JavaScript">
function changeRed( )
{
document.bgColor="red";
}
function changeGreen( )
{
document.bgColor="green";
}
function changeBlue( )
{
document.bgColor="blue";
}
</script>
</head>
<body bgcolor="#ccddee">
<center>
<br>
<br>
<form name="myform">
<input type="button" name="cmdred" onClick="changeRed( )" value="Red">
<input type="button" name="cmdgree" onClick="changeGreen( )" value="Green">
<input type="button" name="cmdblue" onClick="changeBlue( )" value="Blue">
</form>
</center>
</body>
</html>
output :
<html>
<head>
<title>Using Event Handling</title>
<script language="JavaScript">
function show( )
{
var sname;
sname=document.myform.txtname.value;
Q Design the application to apply the following validation on the user name .
Q Design the application which contains three textboxes for two number and a result , and
perform +,-,*,/ and %.
<html>
<head>
<title>Using Event Handling in JavaScript</title>
<script language="JavaScript">
function add( )
{
var a,b,c;
a=document.myform.txta.value;
b=document.myform.txtb.value;
a=parseInt(a);
b=parseInt(b);
c=a+b;
document.myform.txtc.value=c;
}
function subtract( )
{
var a,b,c;
a=document.myform.txta.value;
b=document.myform.txtb.value;
a=parseInt(a);
b=parseInt(b);
c=a-b;
document.myform.txtc.value=c;
}
function multiply( )
{
var a,b,c;
a=document.myform.txta.value;
b=document.myform.txtb.value;
a=parseInt(a);
b=parseInt(b);
c=a*b;
document.myform.txtc.value=c;
}
function divide( )
{
var a,b,c;
a=document.myform.txta.value;
b=document.myform.txtb.value;
a=parseInt(a);
b=parseInt(b);
c=a/b;
document.myform.txtc.value=c;
}
function mod( )
{
var a,b,c;
a=document.myform.txta.value;
b=document.myform.txtb.value;
a=parseInt(a);
b=parseInt(b);
c=a%b;
document.myform.txtc.value=c;
}
</script>
</head>
<body bgcolor="#ccffee">
<center>
<br>
<br>
<form name="myform">
<b> Enter the value of a : </b><input type="text" name="txta"><br>
<b> Enter the value of b : </b><input type="text" name="txtb"><br>
<br>
<input type="button" name="cmdplus" value="+" onClick="add( )"><input type="button"
name="cmdminus" value="-" onClick="subtract( )"><input type="button" name="cmdmult"
value="*" onClick="multiply( )"><input type="button" name="cmddiv" value="/"
onClick="divide( )"><input type="button" name="cmdmod" value="%" onClick="mod( )"><br>
<br>
<b> Result : </b><input type="text" name="txtc">
<br>
</form>
</center>
</body>
</html>
e.g.
x=new Array(5);
<html>
<head>
<title>Using JavaScript</title>
</head>
<body bgcolor="#ccddee">
<script language="JavaScript">
x=new Array(5);
x[0]=10;
x[1]=12;
x[2]=13;
x[3]=14;
x[4]=16;
var i;
for(i=0;i<5;i++)
{
document.write(x[i]+"<br>");
}
</script>
</body>
</html>