SECTION 3
SECTION 3
SECTION 3 JAVASCRIPT
Structure
3.0 Script Basics
3.1 Incorporating JavaScript into a Web Page
3.2 Exercises for Practice in Lab Sessions.
Today’s Web sites include graphics, sound animation, video and sometimes even
useful content! Web scripting languages, such as JavaScript, are one of the easiest
ways to spice up the Web page and to interact with users in new ways, HTML, unlike
which is a simple text markup language, which can’t respond to the user, make
decisions, or automate repetitive tasks. Web scripting languages allow you to
combine scripting with HTML to create interactive Web pages.
The <SCRIPT> tag tells the browser to start treating the text as a script, and the
</SCRIPT> tag returns to the regularly scheduled HTML as given below. Note that
the file will be stored as .html or .htm.
13
Lab Manual
In this example, we placed the script within the body of the HTML document. There
are actually four different places where you can use scripts:
• In the body of the page. In this case, the output of the script is displayed, as part
of the HTML document, when the browser loads the page.
• In the header of the page, between the <HEAD> tags. Scripts in the header
aren’t executed immediately, but can be referred to by other scripts. The header
is often used for functions.
14
• Within an HTML tag. This is called an event handler and allows the script to JavaScript
work with HTML elements. Event handlers are one type of script where you
don’t need to use the <SCRIPT> tag.
• In a separate file entirely JavaScript supports the use of files with the .js
extension containing scripts; these can be included by specifying a file in the
<SCRIPT> tag. This feature works only in Netscape Navigator 3.0 or later and
Internet Explorer 4.0 or later.
2. Embed JavaScript in HTML document asking user’s name and then printing
Hello <User_Name>
SESSION 2
Exercises
1. Evaluate the expression:
a. 7+5
b. “7” + “5”
c. 7*5
d. 7/5
e. 7%5
2. Write the segment of Script that would ask the user if he wants a greeting
message and if he does, display a Gif file called Welcome .gif and display
“Welcome to Netscape Navigator!” in the document window following the Gif
3. Write the object definition for an object called car with four properties model,
make, year & price.
SESSION 3
Exercises
1. Write a program to display a multiplication table.
SESSION 4
Exercises
1. Write a program to move a text with mouse pointer.
15
Lab Manual 3. Create a Web page using two image files, which switch b/w one another as the
mouse pointer moves over the image. Use the On Mouse over and On Mouse
out event handler
SESSION 5
Exercises
1. Write a JavaScript code to accept radius & display the area of the circle.
2. Use the date function get Date & set Date to prompt the user for an integer b/w
1 – 31 & return day of the week it represents.
3. Display time and print message accordingly e.g., ‘Good Morning’ in Morning
etc.
SESSION 6
Exercises
1. Using JavaScript create a digital clock.
SESSION 7
Exercises
1. Using JavaScript create a calculator.
SESSION 8
Exercises
1. Create an HTML form that has a number of text boxes. The user fills the
textboxes with data. Write a script that verifies that all textboxes have been
filled. If a text box has been left empty pop up an alert message indicating the
box that has been left empty. When OK button is clicked, set focus to that
specific textbox. If all the textboxes are filled, display thank you.
SESSION 9
Exercises
1. Create an HTML form that inputs employee details and display the same on the
HTML page.
2. Write a program, which prompts the user to enter a sum of two numbers and
display whether the answer is correct or incorrect.
SESSION 10
Exercises
1. Illustrate how the reset button on form functions.
SESSION 11
Exercises
1. Create a program to generate a hit counter.
16
2. Create a program to verify whether email address provided by the user is valid JavaScript
or invalid.
SESSION 12
Exercises
1. Write a program to scroll the text on status bar.
SESSION 13
Exercises
1. The form consists of two multiple choice lists and one single choice list
a. The first multiple choice list displays the major dishes available.
b. The second multiple choice list displays the stocks available.
c. The single choice list displays the miscellaneous (Milkshakes, soft drinks,
softy etc. available)
SESSION 14
Exercises
1. Create a Web page with two forms, one office copy and one customer copy
when user enters date in customer copy it gets updated in office copy.
SESSION 15
Exercises
1. Use JavaScript for authentication and verification of the textboxes in the static
site developed by the student in the HTML exercise.
17