Chapter 6-Client Side Scripting Using JavaScript-hsslive
Chapter 6-Client Side Scripting Using JavaScript-hsslive
Chapter 6
Java Script
Java script is a client side scripting language used to validate data.It is embedded in
HTML document.The <SCRIPT> tag is used to include scripts in an HTML
page.JavaScript was developed by Brendan Eich.It is ssupported by almost all web
browsers. JavaScript was first known as LiveScript.
<SCRIPT> Tag
Example:
<SCRIPT Language=”JavaScript”>
</SCRIPT>
The function document.write which writes a string into our HTML document.
<body>
document.write("Welcome to JavaScript") ;
</script>
</body>
</html>
Note:- We use Notepad for creating the above script and save the file with extension
.HTML or .HTM. JavaScript ignores spaces, tabs, and newlines that appear in JavaScript
programs.Java-
Defining a function
Functions must be declared before they are used.A function is defined using the
function keyword.The function statements are emclosed in curley brace.Once a function
is defined it can be called many times.A function has two parts function header and
function body.
function print( )
Calling a function
Data type specifies the type of data and the operations that can be performed on the
data.data types in JavaScript is classiffied into two primitive data type and compositive
data type.
The three primitive data types in JavaScript are Number,String and Boolean.
Variables in JavaScript
Example: var a;
Declares a variable a.
The Undefined data type is used to represent a variable that has been named, but has
had no data assigned to it. The undefined data type is similar to null, in that it only has
Example:-
<HTML>
<HEAD>
<TITLE>JAVASCRIPT_DEMO</TITLE>
<SCRIPT Language="JavaScript">
function sum()
var a,b,c;
a=10;
b=2;
c=a+b;
document.write("Sum is");
document.write(c);
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT Language="JavaScript">
sum( );
</SCRIPT>
</BODY>
</HTML>
Output:-
Operators in JavaScript
Arithmetic Operator
Relational Operator
Assignment Operator
The arithmetic assignment operators includes +=,- =,* =,/ =,% =.They are used to simplify
the use of assignment operator.
Example:-
Logical Operator
Given that x = 6 and y = 3, the table below explains the logical operators:
Operator Precedence
() Expression grouping
String addition
Example:-
var a,b,c
a=”Hello”;
c=a+b;
document.write(c);
Output:-
Example:
Number(“42”); //returns 42
Number(“eggs”); //returns NaN String that can’t be converted to number retyrns NaN.
Number(true);//returns 1
Number(false);//returns 0
Note:-
Control structures are used to alter the normal sequence of execution of a program.The
important
1)if statement
if(test_expression)
Statements;
2)Switch statement
switch(expression)
case value1:statement1;break;
case value2:ststement2;break;
- -----------------------
Default:statement;
3)for ............Loop
for(initialisation;expression;update_statement)
statements;
while(expression)
statements;
Built-in Functions
Built-in functions are also called methods.The following are the important methods in
JavaScript.
1)alert( ) function
alert(“message”);
2)isNaN( ) function
The isNaN( ) function is check if a value is a number or not.The function returns True if
the value is a number.The syntax is
isNaN(test_value);
3)toUpperCase ( ) function
Example:-
var a,b;
a=”abcd”;
b=a.toUpperCase( );
document.write(b);
Output:ABCD
4)toLowerCase( ) function
5)charAt( ) function
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.
Example:-
returns H
length Property
Example:-
var a=”Welcome”;
var len=a.length;
document.write(len);
Output:-
A function has paremeters within parenthesis after the function name,but property
doesnot have paremeters within parenthesis.
Events in JavaScript
Event Description
onClick Occurs when the user clicks on an object
onMouseEnter Occurs when the mouse pointer is moved
out of an object.
onMouseLeave Occurs when the mouse pointer is moved
out of an object.
onKeyDown Occurs when the user press a key on the
keyboard.
onKeyUp Occurs when the user releases a key on the
keyboard.
var n= document.txtnum.value;
JavaScript code can be placed in the Head or Body section of a web page.It can also be
placed as an external file with ‘.js’ extension.Placing JavaScript code as external file
helps to use it in multiple web pages.It also helps to load the page faster.The external
script can be linked to HTML file by the Src attribute of <Script> tag.
Placing JavaScript in head section allows to execute the script faster as the head section
is loaded before the body section.Placing the script in Body section allows to execute the
script when the page is loaded.
Conclusion:-
Previous Questions
1.Develop a webpage to display the following login screen.
1)Develop a web page to display the following screen.User can enter a number in the
first text box.On clicking the show button ,product of all numbers from 1 to the entered
limit should be displayed in the second text box.
<html>
<head>
<Script Language="JavaScript">
function Product()
var p=1,i;
var n=document.frmProduct.txtNum.value;
for(i=1;i<=n;i++)
p=p*i;
document.frmProduct.txtResult.value=p;
</Script>
</head>
<body>
<center>
<form name="frmProduct">
<br><br><br>
<br><br><br>
</form>
</body>
</center>
</html>
Output:-
2)Develop a web page to display the following screen.User anter a number in the first
text box.On clicking the show button,Even or Odd should be displayed in the second text
box depending whether the number is even or odd.
<html>
<head>
<Script Language="JavaScript">
function Check()
var n=document.frmCheck.txtNum.value;
if(n%2==0)
document.frmCheck.txtResult.value="Even";
else
document.frmCheck.txtResult.value="Odd";
</Script>
</head>
<body>
<center>
<form name="frmCheck">
<br><br><br>
<br><br><br>
</form>
</body>
</center>
</html>
Output:-
3)Develop a web page to display the following screen.The user can enter an age in the
text box.if the user enter an alphabet,instead of a number in the text box,on clicking the
show button,it should display a message “Invalid Age” to the user.Other wise it should
display a message “Correct Data”.
<html>
<head>
<Script Language="JavaScript">
function Check()
var age=document.frmCheck.txtNum.value;
var flag;
flag=isNaN(age);
if(flag==true)
alert("Invalid Age");
else
alert("Correct Data");
</Script>
</head>
<body>
<center>
<form name="frmCheck">
<br><br><br>
</form>
</body>
</center>
</html>
Output:-
4) Develop a web page to display the following screen.The user can enter a number in
the text.If the user entered a Number ,on clicking the check button,it should display
“Number” other wise it should display “Not a Number”.
<HTML>
<HEAD>
<TITLE>Calculator</TITLE>
<SCRIPT Language="JavaScript">
function Check( )
var num;
var Flag;
num=document.frmCheck.txtNum.value;
Flag=isNaN(num);
if(Flag==true)
document.frmCheck.txtResult.value="Not a Number";
else
document.frmCheck.txtResult.value="Number";
</SCRIPT>
</HEAD>
<FORM name=frmCheck>
<CENTER>
Enter a Number:
<BR><BR>
Result:
</CENTER>
</FORM>
</BODY>
</HTML>
Output:-
5) A web page should contain one text box for entering a text. There should be two
buttons labelled "To Upper Case" and "To Lower Case".On clicking each button, the
content in the text box should be converted to upper case or lower case accordingly.
Write the required
<HEAD>
<TITLE>String_Convertion</TITLE>
<SCRIPT Language="JavaScript">
function upper( )
document.frmconvert.txtconvert.value=document.frmconvert.txtstring.value.toUpperCa
se();
function lower( )
document.frmconvert.txtconvert.value=document.frmconvert.txtstring.value.toLowerCa
se();
</SCRIPT>
</HEAD>
<BODY>
<FORM Name="frmconvert">
<CENTER>
<BR><BR>
<BR><BR>
</CENTER>
</FORM>
</BODY>
</HTML>
Output:-
6) Develop a simple calculator using JavaScript. The web page should contain two text
boxes of entering two numbers and another text box for displaying the answer. There
should be four buttons to perform addition, subtraction, multiplication and division. On
clicking a button, the corresponding result should be displayed in the answer box. Write
the required JavaScript.
<HTML>
<HEAD>
<TITLE>Calculator</TITLE>
<SCRIPT Language="JavaScript">
function plus( )
{
var num1,num2,result;
num1=document.frmcalc.txtNum1.value;
num2=document.frmcalc.txtNum2.value;
result=Number(num1)+Number(num2);
document.frmcalc.txtResult.value=result;
}
function minus( )
{
var num1,num2,result;
num1=document.frmcalc.txtNum1.value;
num2=document.frmcalc.txtNum2.value;
result=Number(num1)-Number(num2);
document.frmcalc.txtResult.value=result;
}
function div( )
{
var num1,num2,result;
num1=document.frmcalc.txtNum1.value;
num2=document.frmcalc.txtNum2.value;
result=Number(num1)/Number(num2);
document.frmcalc.txtResult.value=result;
}
function mul( )
{
var num1,num2,result;
num1=document.frmcalc.txtNum1.value;
num2=document.frmcalc.txtNum2.value;
result=Number(num1)*Number(num2);
document.frmcalc.txtResult.value=result;
}
</SCRIPT>
</HEAD>
<FORM name=frmcalc>
<CENTER>
Number 1
<INPUT Type="text" name="txtNum1">
<BR><BR>
Number 2
<INPUT Type="text" name="txtNum2">
<BR><BR>
Answer
7) Develop a web page to find the capital of Indian States. The page should contain a
dropdown list from which the user can select a state.On clicking the show button, the
web page should display the capital of the state in another text box. Write the required
JavaScript.
<HTML>
<HEAD>
<TITLE>Capital Of States</TITLE>
<SCRIPT Language="JavaScript">
function Capital( )
{
var n,answer;
n=document.frmCapital.cboState.selectedIndex;
switch (n)
{
case 0: answer="Thiruvananthapuram";break;
case 1:answer="Bengaluru";break;
case 2:answer="Chennai";break;
case 3:answer="Mumbai";break;
}
document.frmCapital.txtCapital.value=answer;
}
</SCRIPT>
</HEAD>
<FORM Name="frmCapital">
<CENTER>State:
<SELECT Size=1 Name="cboState">
<OPTION>Kerala</OPTION>
<OPTION>Karnataka</OPTION>
<OPTION>Tamilnadu</OPTION>
<OPTION>Maharashtra</OPTION>
</SELECT>
<BR><BR>
Capital:
<INPUT Type="Text" Name="txtCapital">
<BR><BR>
<INPUT Type="Button" value="Show" onClick="Capital ( )">
</CENTER>
</FORM>
</BODY>
</HTML>
Output:-
8)Develop a web page to accept a Register Number of a student.Provide validation for this
textbox.The validation are 1)It should not be empty 2)It should be a number 3)It should be
greater than 8 characters.
<html>
<head>
<title>Javascript-Validation</title>
<Script Language="Javascript">
function checkData()
var rno=document.frmvalid.txtRegno.value;
if(rno==" ")
if(isNaN(rno))
if(rno.length<8)
</Script>
</head>
<body>
<form name="frmvalid">
<br>
</form>
</body>
</html>
Output:-
9)Develop a webpage to find the factorial of a given number .User can enter a number
in a text box.On clicking the Show button factorial should be displayed on second text
box.
<html>
<head>
<Script Language="JavaScript">
function factorial()
var f=1,i;
var n=document.frmfact.txtNum.value;
for(i=1;i<=n;i++)
f=f*i;
document.frmfact.txtResult.value=f;
</Script>
</Head>
<body>
<center>
<form name="frmfact">
Factorial:
<br>
</form>
</body>
</center>
</html>
Output:-