0% found this document useful (0 votes)
45 views5 pages

CA2 Chapter 6

Uploaded by

bisheraysha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views5 pages

CA2 Chapter 6

Uploaded by

bisheraysha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computer Applications (Commerce) – XII Chapter 6

Chapter 6
Client side Scripting using JavaScript

JavaScript
It is a client side scripting language. The script or program code is written in HTML file within
<SCRIPT> tag pair. The script is mainly used for data validations in the Forms at the client
side. The <SCRIPT> tag uses Language attribute and its value will be JavaScript. Every
browser has a JavaScript engine. The script code is interpreted at runtime by the JavaScript
engine.
A sample JavaScript function is given below. It is usually written in <HEAD> section.
<SCRIPT Language= "JavaScript">
function print()
{
document.write("Welcome to JavaScript.");
}
</SCRIPT>
Name of this function is print() and it can be called by the following code segment:
<BODY>
<SCRIPT Language= "JavaScript">
print();
</SCRIPT>
</BODY>
When this html file is opened in a browser, we can see Welcome to JavaScript in the
browser window. The keyword document refers to the web page being opened and
write() is a built-in function in JavaScript to display a text.

Data Types in JavaScript


(i) Number: All numbers fall into this category.
(ii) String: Any combination of characters, numbers or any other symbols, enclosed within double
quotes.
(iii) Boolean: Only two values fall in this type. They are true and false. Note that the values are
not in double quotes.

Variables
Variables are used for storing values. They are declared using the keyword var as: var x;
The variable definition is complete only when it is assigned a value as follows.
var x, y;
x = 25;
y = “INDIA”;
In the above example, the variable x is of Number type and y is of String type.

Operators
Arithmetic operators + – * / %
Increment, decrement ++ ––
Assignment operators = += –= *= /= %=
Relational operators < <= > >= == !=
Logical operators || && !
String concatenation +
1
Joy John’s CA capsules
Computer Applications (Commerce) – XII Chapter 6

An example for String concatenation:


x = “Java”;
y = “Script”;
z = x + y;
The + operator will add the two strings and the variable z will have the value JavaScript.

Another example:
x = “23”;
y = 5;
z = x + y;
The value of z will be 235. But the output of the statement z = Number(x) + y; will be
28. The Number() function coverts the string “23” into the number 23.

Control Statements
if (test_expression)
Statement;
if (test_expression)
statement_1;
else
statement_2;
if (test_expression1)
if statements
statement_1;
else if (test_expression2)
statement_2;
:
:
else
statement_n;
switch (variable/expression)
{
case value1: statement1; break;
switch case value2: statement2; break;
statement :
:
default: statement;
}
for (initialization; test; update)
for loop
body;
initialization;
while (test_expression)
{
while loop
body;
update;
}

2
Joy John’s CA capsules
Computer Applications (Commerce) – XII Chapter 6

Built-in Functions
Function Use Syntax / Example
alert() To display a text in a message window. alert(“Welcome”);
isNaN() Returns True if the given value is not a isNaN(“welcome”); and
number. That is, the argument contains a isNaN(“A123”); return True.
non-numeric character. Returns False is the isNaN(“13”); and isNaN(13);
return False
argument is numeric.
toUpperCase() Returns the upper case form of the given Output of
string. “Java”.toUpperCase(); will
be JAVA.
toLowerCase() Returns the lower case form of the given Output of
string. “JavaSCIPT”.toLowerCase();
will be javascript.
charAt() Returns the character at a particular “JavaScript”.charAt(4); gives
th
position. charAt(0) gives the 1st character of S, the 5 character.
a string.
length Returns the length (number of characters) “JavaScript”.length will give
property of the string. 10.

Accessing Values from the Input controls of a Form


The data from a text box in a Form is accessed by the following format:
Variable = document.Form_Name.TextBox_Name.Value;
Form_Name is the value of the Name attribute of the Form. TextBox_Name is the value of
the Name attribute of the text box. The data in the text box is stored as String type even
though we input a number. The data is to be converted into numeric using Number()
function is necessary.

Consider the following line of code:


<INPUT Type= "button" Value= "Show" onClick= "showSquare()">
Here, onClick= "showSquare()" means that when the user clicks this button in the
Form, the function with the name showSquare() is called.

Ways to add JavaScript to a Web Page


Inside the <BODY>: Here, the scripts will be executed while the content of the web page is being
loaded.
Inside the <HEAD>: Usually functions are written here. They are called from body. The script in this
section will be loaded first. Mixing of web page content and scripts can be avoided also.
External JavaScript file: Scripts can be placed into an external file and it can be linked to with the
HTML document. This file is saved with the extension ‘.js’. The advantage is that the same script can
be used across multiple HTML pages or a whole website. The linking is done through the attribute
Src of <SCRIPT> tag.

3
Joy John’s CA capsules
Computer Applications (Commerce) – XII Chapter 6

Questions from Previous Years’ Question Papers (Computer Applications)


1. Develop a web page to display the following login screen.
Application No.
Password
Login

Write JavaScript to do the following validation:


(a) The application number should be in the range 10000 to 99999.
(b) The password should contain at least 8 characters. (3) (March 2016)
2. Design the following web page to enter the mark of a student:
Mark
Check
(a) Write HTML code for the web site.
(b) Provide validations for the text box using JavaScript. The mark should be in the
range 0 to 100 and should be a number. The textbox should not be empty.
(3) (SAY 2016)
3. What is an external JavaScript file? Write the advantage of using an external JavaScript
file. (3) (March 2017)
4. Develop a web page to display the following screen:

Enter Name
Show

The user can enter a name in the textbox. On clicking the ‘show’ button the name
entered should be changed into uppercase. Include JavaScript code in the HTML for
doing this. (3) (March 2017)
5. Write a JavaScript which inputs the name, rollno and date_of_birth of a student. Date
of birth contains month, day and year. Month should be selected from a drop-down list.
(3) (SAY 2017)

4
Joy John’s CA capsules
Computer Applications (Commerce) – XII Chapter 6

Questions from Previous Years’ Question Papers (Computer Science)


1. “TRUE and False are used to represent Boolean values”. State if the given statement is
correct or not. (1) (March 2016)
2. Explain the use of for loop with an example. (3) (March 2016)
3. Develop a web page that implements a JavaScript function that takes two numbers as
input and displays their product. (2) (March 2016)
4. Give the function in JavaScript that converts a string type data containing numbers to
number type. (1) (SAY 2016)
5. Design a web page with form tag which accepts a number in a textbox and another
textbox which should display either odd or even. Write a function in JavaScript to check
whether the number is odd or even. (2) (SAY 2016)
6. Develop a web page that accepts a number after validation and prints the factorial of it.
(2) (SAY 2016)
7. JavaScript provides a large number of built-in functions.
(a) Name any two of them with an example. (2) (SAY 2016)
(b) The property which returns the size of the string is _____. (1) (SAY 2016)
8. A virtual machine for executing JavaScript code is _____. (1) (March 2017)
9. Discuss about six built-in functions used in JavaScript. (3) (March 2017)
10. Design a procedure in JavaScript that takes two strings as input and displays the
concatenated string as output. (2) (March 2017)
11. State whether the following statements are true or false:
(a) JavaScript is the only client side scripting language.
(b) JavaScript is a case sensitive language.
(c) The keyword used to declare a variable in JavaScript is VAR. (3) (SAY 2017
12. Predict the output of the following code:
<HTML>
<BODY>
<SCRIPT Language="JavaScript">
var i, s=0;
for (i=1;i<=10; i+=2)
s=s+i;
document.write("sum="+s);
</SCRIPT>
</BODY>
</HTML> (2) (SAY 2017)

5
Joy John’s CA capsules

You might also like