WT 2 Jwfiles
WT 2 Jwfiles
com
UNIT-II
Overview
Client-side scripting generally refers to the class of computer programs on the web that
are executed client-side, by the user's web browser, instead of server-side (on the web
server) This type of computer programming is an important part of the Dynamic HTML
(DHTML) concept, enabling web pages to be scripted; that is, to have different and
changing content depending on user input, environmental conditions (such as the time of
day), or other variables.
JavaScript was originally designed in order to add interactivity to the Web Pages
or HTML pages. Previously, the interactivity was achieved through the use of
forms made in JavaScript but later JavaScript was used to add more dynamic and
interactive features. JavaScript, as mentioned before, is a scripting language
which falls under the category of light-weight programming languages. Using
JavaScript is quite easy as the web programmer can easily embed the JavaScript
code into the HTML pages. Another interesting fact about JavaScript is that it
doesn’t require the user to purchase any license. Another advantage of using
JavaScript is that it is already interpreted, which is the reason it is also called as
an interpreted language. Interpreted language means that the JavaScript execute
simply without any preliminary compilation; which is another reason why it is
called as light-weight programming language.
www.jntuworld.com
www.jntuworld.com
Objectives
Introduction
Client-side scripts are often embedded within an HTML document (hence known as an
"embedded script"), but they may also be contained in a separate file, which is referenced
by the document (or documents) that use it (hence known as an "external script"). Upon
request, the necessary files are sent to the user's computer by the web server (or servers)
on which they reside. The user's web browser executes the script, then displays the
document, including any visible output from the script. Client-side scripts may also
contain instructions for the browser to follow if the user interacts with the document in a
certain way, e.g., clicks a certain button. These instructions can be followed without
further communication with the server, though they may require such communication.
By viewing the file that contains the script, users may be able to see its source code.
Many web authors learn how to write client-side scripts partly by examining the source
code for other authors' scripts.
In contrast, server-side scripts, written in languages such as Perl, PHP, and server-side
VBScript, are executed by the web server when the user requests a document. They
produce output in a format understandable by web browsers (usually HTML), which is
then sent to the user's computer. The user cannot see the script's source code (unless the
author publishes the code separately), and may not even be aware that a script was
executed. The documents produced by server-side scripts may, of course, contain client-
side scripts.
Client-side scripts have greater access to the information and functions available on the
user's browser, whereas server-side scripts have greater access to the information and
functions available on the server. Server-side scripts require that their language's
interpreter be installed on the server, and produce the same output regardless of the
client's browser, operating system, or other system details. Client-side scripts do not
require additional software on the server (making them popular with authors who lack
administrative access to their servers); however, they do require that the user's web
browser understands the scripting language in which they are written.
www.jntuworld.com
www.jntuworld.com
It is therefore impractical for an author to write scripts in a language that is not supported
by the web browsers used by a majority of his or her audience.
Due to security restrictions, client-side scripts may not be allowed to access the user's
computer beyond the web browser application. Techniques like ActiveX controls can be
used to sidestep this restriction.
Unfortunately, even languages that are supported by a wide variety of browsers may not
be implemented in precisely the same way across all browsers and operating systems.
Authors are well-advised to review the behavior of their client-side scripts on a variety of
platforms before they put them into use.
To begin, let's look at a simple image map I created. If you click the left half of the
image, you will be taken to a tables tutorial. If you click the right side, you'll get a tutorial
on frames. Both the links are on one single image, so we just call it an image map. Try it
out:
So, how do we create one of these? First, you need to have an image you want to use. Just
pick one you like, or make your own. Next, you need to know the width and height of the
image you are using. If you are not sure, open the image in your image editing program.
You should have some option that will tell you the width and height of the image.
Now you need to put the image on the page. To do this, you use the image tag, but with a
new attribute: usemap.
The usemap="#mymap" command tells the browser to use a map on the page, which is
named "mymap". Notice how it uses the "#" symbol in front of the map name. Also
notice that we defined the width and height of the image. This needs to be done so we can
use coordinates later on when we define the map. Speaking of that, lets see how to define
www.jntuworld.com
www.jntuworld.com
the map. For this map, we would place the following code somewhere on the page. I just
put it right after the <img> tag to make it easy to find.
Now you can see where the usemap="#mymap" from the <img> tag comes from. The
name of the map is "mymap". Now, let's look at what all of this means:
www.jntuworld.com
www.jntuworld.com
left out. We didn't leave out anything in this map, but if we had, this would be the
URL someone would go to if they clicked on any area we did not define earlier.
</map>
This ends the map section!
Now, you can use other shapes besides rectangles, but those are alot tougher to code by
hand. I would suggest using a shareware image map creation program if you use other
shapes in your map.
So, if you need a simple image map separated into rectangular sections, just do what we
did above. You can have as many rectangular areas as you want, just add the extra area
tags with coordiantes and urls. Sounds like fun, don't you think?
JavaScript is the most popular scripting language on the internet, and works in all
major browsers, such as Internet Explorer, Firefox, Chrome, Opera, and Safari.
Before you continue you should have a basic understanding of the following:
HTML / XHTML
If you want to study these subjects first, find the tutorials on our Home page.
What is JavaScript?
NO!
Java and JavaScript are two completely different languages in both concept and design!
www.jntuworld.com
www.jntuworld.com
The language was invented by Brendan Eich at Netscape (with Navigator 2.0), and has
appeared in all Netscape and Microsoft browsers since 1996.
The development of ECMA-262 started in 1996, and the first edition of was adopted by
the ECMA General Assembly in June 1997.
The standard was approved as an international ISO (ISO/IEC 16262) standard in 1998.
www.jntuworld.com
www.jntuworld.com
The HTML <script> tag is used to insert a JavaScript into an HTML page.
JavaScript
JavaScript-specific
JavaScript is officially managed by Mozilla, and new language features are added
periodically. However, only some non-Mozilla "JavaScript" engines support these new
features:
The primary use of JavaScript is to write functions that are embedded in or included from
HTML pages and interact with the Document Object Model (DOM) of the page. Some
simple examples of this usage are:
Opening or popping up a new window with programmatic control over the size,
position, and attributes of the new window (i.e. whether the menus, toolbars, etc.
are visible).
Validation of web form input values to make sure that they will be accepted
before they are submitted to the server.
Changing images as the mouse cursor moves over them: This effect is often used
to draw the user's attention to important links displayed as graphical elements.
www.jntuworld.com
www.jntuworld.com
Because JavaScript code can run locally in a user's browser (rather than on a remote
server) it can respond to user actions quickly, making an application feel more
responsive. Furthermore, JavaScript code can detect user actions which HTML alone
cannot, such as individual keystrokes. Applications such as Gmail take advantage of this:
much of the user-interface logic is written in JavaScript, and JavaScript dispatches
requests for information (such as the content of an e-mail message) to the server. The
wider trend of Ajax programming similarly exploits this strength.
Nonetheless, JavaScript was designed with Java's syntax and standard library in mind. In
particular, all Java keywords are reserved in JavaScript, JavaScript's standard library
follows Java's naming conventions, and JavaScript's Math and Date classes are based on
those from Java 1.0.
Conditional Statements
Very often when you write code, you want to perform different actions for different
decisions. You can use conditional statements in your code to do this.
www.jntuworld.com
www.jntuworld.com
if...else if....else statement - use this statement to select one of many blocks of
code to be executed
switch statement - use this statement to select one of many blocks of code to be
executed
If Statement
Use the if statement to execute some code only if a specified condition is true.
Syntax
if (condition)
{
code to be executed if condition is true
}
Example
<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10
if (time<10)
{
document.write("<b>Good morning</b>");
}
</script>
If...else Statement
Use the if....else statement to execute some code if a condition is true and another code if
the condition is not true.
Syntax
if (condition)
{
code to be executed if condition is true
}
else
www.jntuworld.com
www.jntuworld.com
{
code to be executed if condition is not true
}
Example
<script type="text/javascript">
//If the time is less than 10, you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.
Use the if....else if...else statement to select one of several blocks of code to be executed.
Syntax
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and condition2 are not true
}
Example
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
www.jntuworld.com
www.jntuworld.com
if (time<10)
{
document.write("<b>Good morning</b>");
}
else if (time>10 && time<16)
{
document.write("<b>Good day</b>");
}
else
{
document.write("<b>Hello World!</b>");
}
</script>
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
This is how it works: First we have a single expression n (most often a variable), that is
evaluated once. The value of the expression is then compared with the values for each
case in the structure. If there is a match, the block of code associated with that case is
executed. Use break to prevent the code from running into the next case automatically.
Example
<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.
www.jntuworld.com
www.jntuworld.com
theDay=d.getDay();
switch (theDay)
{
case 5:
document.write("Finally Friday");
break;
case 6:
document.write("Super Saturday");
break;
case 0:
document.write("Sleepy Sunday");
break;
default:
document.write("I'm looking forward to this weekend!");
}
</script>
Alert Box
An alert box is often used if you want to make sure information comes through to the
user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
alert("sometext");
Example
<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("I am an alert box!");
}
</script>
</head>
<body>
</body>
</html>
www.jntuworld.com
www.jntuworld.com
Confirm Box
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to
proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns
false.
Syntax
confirm("sometext");
Example
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button");
if (r==true)
{
document.write("You pressed OK!");
}
else
{
document.write("You pressed Cancel!");
}
}
</script>
</head>
<body>
</body>
</html>
Prompt Box
A prompt box is often used if you want the user to input a value before entering a page.
www.jntuworld.com
www.jntuworld.com
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to
proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box
returns null.
Syntax
prompt("sometext","defaultvalue");
Example
<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
{
document.write("Hello " + name + "! How are you today?");
}
}
</script>
</head>
<body>
</body>
</html>
JavaScript Functions
To keep the browser from executing a script when the page loads, you can put your script
into a function.
A function contains code that will be executed by an event or by a call to the function.
You may call a function from anywhere within a page (or even from other pages if the
function is embedded in an external .js file).
Functions can be defined both in the <head> and in the <body> section of a document.
However, to assure that a function is read/loaded by the browser before it is called, it
could be wise to put functions in the <head> section.
www.jntuworld.com
www.jntuworld.com
Syntax
function functionname(var1,var2,...,varX)
{
some code
}
The parameters var1, var2, etc. are variables or values passed into the function. The { and
the } defines the start and end of the function.
Note: A function with no parameters must include the parentheses () after the function
name.
Note: Do not forget about the importance of capitals in JavaScript! The word function
must be written in lowercase letters, otherwise a JavaScript error occurs! Also note that
you must call a function with the exact same capitals as in the function name.
Example
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>
If the line: alert("Hello world!!") in the example above had not been put within a
function, it would have been executed as soon as the line was loaded. Now, the script is
not executed before a user hits the input button. The function displaymessage() will be
executed if the input button is clicked.
www.jntuworld.com
www.jntuworld.com
The return statement is used to specify the value that is returned from the function.
So, functions that are going to return a value must use the return statement.
The example below returns the product of two numbers (a and b):
Example
<html>
<head>
<script type="text/javascript">
function product (a,b)
{
return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3));
</script>
</body>
</html>
JavaScript Loops
Often when you write code, you want the same block of code to run over and over again
in a row. Instead of adding several almost equal lines in a script we can use loops to
perform a task like this.
www.jntuworld.com
www.jntuworld.com
The for loop is used when you know in advance how many times the script should run.
Syntax
for (var=startvalue;var<=endvalue;var=var+increment)
{
code to be executed
}
Example
The example below defines a loop that starts with i=0. The loop will continue to run as
long as i is less than, or equal to 5. i will increase by 1 each time the loop runs.
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
The while loop loops through a block of code while a specified condition is true.
Syntax
while (var<=endvalue)
{
code to be executed
}
www.jntuworld.com
www.jntuworld.com
Example
The example below defines a loop that starts with i=0. The loop will continue to run as
long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
<html>
<body>
<script type="text/javascript">
var i=0;
while (i<=5)
{
document.write("The number is " + i);
document.write("<br />");
i++;
}
</script>
</body>
</html>
The do...while loop is a variant of the while loop. This loop will execute the block of
code ONCE, and then it will repeat the loop as long as the specified condition is true.
Syntax
do
{
code to be executed
}
while (var<=endvalue);
Example
The example below uses a do...while loop. The do...while loop will always be executed at
least once, even if the condition is false, because the statements are executed before the
condition is tested:
<html>
<body>
<script type="text/javascript">
var i=0;
do
{
www.jntuworld.com
www.jntuworld.com
The break statement will break the loop and continue executing the code that follows
after the loop (if any).
Example
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=10;i++)
{
if (i==3)
{
break;
}
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
The continue statement will break the current loop and continue with the next value.
Example
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=10;i++)
{
if (i==3)
www.jntuworld.com
www.jntuworld.com
{
continue;
}
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
The for...in statement loops through the elements of an array or through the properties of
an object.
Syntax
Note: The code in the body of the for...in loop is executed once for each
element/property.
Note: The variable argument can be a named variable, an array element, or a property of
an object.
Example
<html>
<body>
<script type="text/javascript">
var x;
var mycars = new Array();
mycars[0] = "Saab";
mycars = "Volvo";
mycars = "BMW";
for (x in mycars)
{
document.write(mycars[x] + "<br />");
}
www.jntuworld.com
www.jntuworld.com
</script>
</body>
</html>
However, creating your own objects will be explained later, in the Advanced JavaScript
section. We will start by looking at the built-in JavaScript objects, and how they are used.
The next pages will explain each built-in JavaScript object in detail.
Note that an object is just a special kind of data. An object has properties and methods.
Properties
In the following example we are using the length property of the String object to return
the number of characters in a string:
<script type="text/javascript">
var txt="Hello World!";
document.write(txt.length);
</script>
Methods
In the following example we are using the toUpperCase() method of the String object to
display a text in uppercase letters:
<script type="text/javascript">
var str="Hello world!";
document.write(str.toUpperCase());
</script>
www.jntuworld.com
www.jntuworld.com
What is an Array?
An array is a special variable, which can hold more than one value, at a time.
If you have a list of items (a list of car names, for example), storing the cars in single
variables could look like this:
$cars1="Saab";
$cars2="Volvo";
$cars3="BMW";
However, what if you want to loop through the cars and find a specific one? And what if
you had not 3 cars, but 300?
An array can hold all your variable values under a single name. And you can access the
values by referring to the array name.
Each element in the array has its own ID so that it can be easily accessed.
Create an Array
There are two ways of adding values to an array (you can add as many values as you need
to define as many variables you require).
1:
You could also pass an integer argument to control the array's size:
www.jntuworld.com
www.jntuworld.com
2:
Note: If you specify numbers or true/false values inside the array then the type of
variables will be numeric or Boolean instead of string.
Access an Array
You can refer to a particular element in an array by referring to the name of the array and
the index number. The index number starts at 0.
document.write(myCars[0]);
To modify a value in an existing array, just add a new value to the array with a specified
index number:
myCars[0]="Opel";
document.write(myCars[0]);
Example
<html>
<head>
<script language="JavaScript">
function temp(form)
{
var f = parseFloat(form.DegF.value, 10);
var T = 0;
T = (f - 62.0) * 8.0 / 7.0;
form.DegC.value = T;
}
// done hiding from old browsers -->
</script>
</head>
<body>
<FORM>
www.jntuworld.com
www.jntuworld.com
Review
Summary
JavaScript was originally designed in order to add interactivity to the Web Pages
or HTML pages.
JavaScript is quite easy as the web programmer can easily embed the JavaScript
code into the HTML pages.
Iinteresting fact about JavaScript is that it doesn’t require the user to purchase any
license.
The advantage of using JavaScript is that it is already interpreted, which is the
reason it is also called as an interpreted language. Interpreted language means that
the JavaScript execute simply without any preliminary compilation; which is
another reason why it is called as light-weight programming language.
o conditional catch clauses
o property getter and setter functions
o iterator protocol adopted from Python
o shallow generators/coroutines also adopted from Python
o array comprehensions and generator expressions also adopted from Python
www.jntuworld.com
www.jntuworld.com
Key terms ::
ECMA
Java Script
href
https,http
PERL,PHP,CGI,VB Script
Multiple choices
www.jntuworld.com
www.jntuworld.com
1. A prompt box is often used if you want the user to input a value.
c Mozilla d.Netscape
3. A JavaScript engine is ----------- JavaScript source code and executes the script
accordingly
c both compiler and interpreter that compiles and interprets d. none of the above.
Review questions
2 Can you make ac omparision between Clinetside scripting and server side scripting?
www.jntuworld.com
www.jntuworld.com
6. How many features are supported by java script engine? What are they?
10. What is the syntax for “for loop “in java script?
Lesson lab::
1. Develop static pages (using only HTML) of an online Book store. The pages should
resemble: www.amazon.com
The website should consist of the following pages.
Home page, Registration and user Login, User profile page, Books catalog, Shopping
cart, Payment By credit card, order confirmation.
2. Validate the registration, user login, user profile and payment by credit card pages
using JavaScript.
www.jntuworld.com