Professional Documents
Culture Documents
Javascript – Part I
1
Introduction
• What is Javascript?
¾An object-oriented scripting language that
is designed primarily for people who are
building web pages using HTML.
¾Javascript programs are embedded within
HTML documents in source code form.
¾The script is interpreted by the browser.
Platform independence.
2
• Javascript is object-oriented.
¾It allows interaction with the properties of
objects that it recognizes.
Internal built-in objects (e.g. window object).
Browser objects (e.g. document object).
• Javascript programs can run both on the
client and the server sides.
¾Client side:
by the browser after downloading the HTML
document.
¾Server side:
requires Netscape Livewire environment.
3
¾Javascript is a small language and does
not have many features that exist in Java.
¾Java is a powerful language and can be
used in extremely sophisticated
applications.
4
¾In the object-oriented paradigm, methods
refer to functions that can be used to
manipulate objects and their properties.
Example: The method write(), which
when invoked on the document object,
causes a specific string of characters to
be outputted.
5
¾Apart from the pre-defined methods, it is
also possible to create user-defined
methods.
Control the rate with which a line of text scrolls
across the screen.
Determine the path of an animated object
across the display.
¾Event handlers can be coded in Javascript.
They are triggered in response to certain
conditions in some objects.
Can be used to control the sequence of
activities in response to some object state.
Running Scripts
6
¾Language=“JavaScript” is optional.
¾For browsers that do not understand
Javascript, it is possible to use the HTML
comment tag “<!--” and “-->” to bracket
out the Javascript code.
The marked block is treated as hidden by the
browsers that do not understand Javascript.
These tags are ignored by browsers that can
interpret Javascript.
<HTML>
<HEAD>
<TITLE> For Illustration </TITLE>
<SCRIPT LANGUAGE = “JavaScript”>
<!-- ……… ………. -->
</SCRIPT>
</HEAD>
<BODY> ……… ………. </BODY>
</HTML>
7
Javascript Examples
Example 1
<HTML>
<TITLE> Displaying Text </TITLE>
<BODY>
<SCRIPT>
document.writeln ("<H1>Hello Good Day</H1>");
document.writeln ("<H3>Best of Luck.</H3>");
</SCRIPT>
</BODY>
</HTML>
>
8
Javascript Confirm Box
Example 2
<SCRIPT>
function respond()
{
confirm (“Hello there!”);
}
</SCRIPT>
<FORM>
<INPUT TYPE="button" VALUE="Click and See"
onClick=“respond()" >
</FORM>
>
9
More on Confirm Box
>
10
Javascript Alerts
Example 4 :: alert
<SCRIPT>
function respond()
{
alert (“Hello!!”);
alert (“Good Day”);
window.location = “http://www.iitkgp.ac.in”;
}
</SCRIPT>
<FORM>
<INPUT TYPE="button" VALUE="Click Here"
onClick="respond()" >
</FORM>
>
11
Changing Background Color
12
Example 6 :: read user input
<SCRIPT>
document.writeln ("<H2> Guess the Weather /H2>");
function forecast (f)
{
a = confirm ("Will it rain today?");
if (a) f.option.value = "RAIN"
else f.option.value = "NO RAIN";
}
</SCRIPT>
<FORM>
<INPUT TYPE="button" VALUE="Click"
onClick="forecast(form)" >
<P> Forecast: <INPUT TYPE="text" NAME="option" SIZE="7" >
</FORM>
>
13
Example 7 :: temperature conversion
<SCRIPT>
function c_to_f (myform)
{
var cent = parseFloat (myform.C.value);
fahr = (cent * 1.8) + 32;
myform.F.value = fahr;
}
</SCRIPT>
<FORM>
Centigrade: <INPUT TYPE=text NAME=C SIZE=6> <P>
<INPUT TYPE="button" VALUE="Convert"
onClick="c_to_f(this.form)">
<P> Fahrenheit: <INPUT TYPE=text NAME=F SIZE=6>
</FORM>
>
14
Example 8
<SCRIPT>
function cat(f)
{
if (f.favcolor[0].selected)
alert ("Favorite color is red");
if (f.favcolor[1].selected)
alert ("Favorite color is yellow");
if (f.favcolor[2].selected)
alert ("Favorite color is green");
}
</SCRIPT>
Example 8 (contd.)
<FORM>
My favorite color:
<SELECT NAME="favcolor">
<OPTION> Red
<OPTION> Yellow
<OPTION> Green
</SELECT>
<P> <INPUT TYPE="button"
VALUE="Done"
onClick="cat(this.form)" >
</FORM>
>
15
Example 9 :: using arrays
<HTML>
<HEAD>
<TITLE> List most popular sportspersons </TITLE>
<SCRIPT LANGUAGE="JavaScript">
sport = new Array(3);
sport[0] = "Sachin Tenduldar";
sport[1] = "Sania Mirza";
sport[2] = "Vishwanathan Anand";
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT>
var number = sport.length
document.write ("<H2>Best sportspersons of
India </H2>" + "<P>");
for (i=0;i<number;i++)
document.write ("Rank " + (i+1) + ": " + sport[i]
+ "<P>");
</SCRIPT>
</BODY>
</HTML>
>
16
SOLUTIONS TO QUIZ
QUESTIONS ON
LECTURE 24
17
Quiz Solutions on Lecture 24
18
Quiz Solutions on Lecture 24
19
Quiz Solutions on Lecture 24
#!/usr/bin/perl -wT
use CGI qw(:standard);
print header (“text/html”);
print start_html ("Hello World");
print "<h2>Hello, world!</h2>\n";
print end_html;
QUIZ QUESTIONS ON
LECTURE 25
20
Quiz Questions on Lecture 25
21