6 Client Side Scripting Using JavaScript
6 Client Side Scripting Using JavaScript
2. Which tag is used for include script in an HTML program? Explain its attribute?
Example:-
<SCRIPT Language=”JavaScript”>
Or
<Script type = ”text/JavaScript” Src = “validation.js”>
document.write( )
Calling a function
A function can be called using its name such as, print( ).
Boolean:-A boolean data can be either True or False( Without double quotes).
Eg. var n;
Data type of the variable is decided only when a value is assigned to it.
n=10;
%= x %= y x=x%y
Var x ,y ,z;
X = ”Hello ”;
Y = ” Good Morning”;
Z = x + y;
document.write(z);
Output:-
Hello Good Morning
12. Write the output of the following javascript code. Give reason
Var x, y, z ;
X = “10”;
Y = 1;
Z = x + y;
document.write(z);
Ans. 101
Variable x is a string. So string addition takes place as y is also treated as string.
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
4
13. Write the output of the following javascript code. Give reason
Var x, y, z;
X = “10”;
Y = 1;
Z = number(x) + y;
document.write(z);
Ans. 11
The number() function convert the string variable containing number. So numeric addition
takes place.
1). if statement
The if statement executes a group of statements based on a condition.The syntax
is
if(test_expression)
{
statements;
}
switch(expression)
{
case value 1 :
statement 1;
break;
case value 2:
statement 2;
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
5
break;
… …. ….. …. ….
…. ….. …. ………
default:
statement;
}
<HTML>
<HEAD><TITLE>Javascript - switch</TITLE>
<SCRIPT Language="JavaScript">
function sw()
var d;
d=document.frmday.txtd.value;
switch(d)
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
6
case 6:
break;
case 7:
break;
default:
</script>
</HEAD>
<BODY>
<FORM Name="frmday">
<CENTER>
<BR><BR>
</CENTER>
</form>
</BODY>
</HTML>
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
7
var n, s;
{
s=n*n;
</script>
while(expression)
{
statements;
}
var n, s;
n=1;
{
s=n*n;
s=n*n;
n+=1;
</script>
Javascript provides a large number of built in functions ( also known as methods). Some of
they are,
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
8
1). alert( )
The alert( ) function is used to display a message on the screen. The syntax is
alert(“message”);
2)isNaN( )
The isNaN( ) function is check if a value is a number or not. The function returns True if
the value is a number otherwise False. syntax is;
isNaN(test_value);
3). toUpperCase ( )
Output : ABCD
4). toLowerCase( )
Output : abcd
5)charAt( )
The charAt() method returns the character at the specified index in a string. The index of
the first character is 0, the second character is 1, and so on.
Eg
Var str = "HELLO WORLD";
var res = str.charAt(0);
returns H
length Property
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
9
16. What are the different methods for adding scripts in an html page?
a). Inside the <Head >section of HTML
Placing the script in <HEAD> tag helps to execute scripts faster as the head
section is loaded before the body section. The main dis-advantage is that scripts that
are to be executed while loading the page will not work when they are placed in the
<HEAD> section.
b). Inside <Body>
Here, the script will be executed while the content of the web page is being
loaded. When the browser sees a script code, it renders the script and then the rest of
the web page is displayed in the browser window.
Scripts can be placed into an external file ,saved with .’js’ extension. It can be used by
multiple HTML pages and also helps to load pages faster. The file is linked to HTML
file using the <SCRIPT> tag.
Eg.,
<SCRIPT Type=”text/JavaScript” Src=”sum.js”>
18. Create a web page which displays the sum of given two numbers using java
script
<html>
<head>
<title> Javasript page</title>
</head>
19. Create a web page which displays simple interest by accepting P, N and R
using JavaScript.
<html>
<head>
<title> Simple interest by Javasript page </title>
<script language = "JavaScript">
function interest();
{
var p, n, r, I;
p=Number(document.frminterest.txtprinciple.value);
n=Number(document.frminterest.txtyear.value);
r=Number(document.frminterest.txtrate.value);
I=p*n*r/100;
document.frminterest.txtinterest.value=I;
}
</script>
</head>
if(age>=18)
{
else
{
document.write(" Minor Person");
return;
}
}
</script>
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
12
</head>
<body bgcolor= f05585>
}
else
{
big=n3;
}
else
{
if(n2>n3)
{
big=n2;
}
else
{
big=n3;
}
}
document.frmlarge.txtbig.value = big;
}
</script>
</body>
</html>
22. Develpe a wep page to display a given string or a number is whether Palindrom or not
using JavaScript.
<html>
<body>
<script type="text/javascript">
function Palindrome()
var i = str.length;
revStr = revStr+str.charAt(j);
if(str == revStr)
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
14
} else
</script>
<form >
</form>
</body>
</html>
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]