Web Technologies Notes
Web Technologies Notes
Web clients view the pages with a program called a web browser.
To publish your work, you web site must be copied to a web server.
Most common is to use an Internet Service Provider (ISP) to host web site.
Renting a server from an Internet Service Provider (ISP) is a common option. Here are some
advantages:
1
Connection Speed
Most ISP providers have very fast connections to the Internet, like full T3 fiber-optic 45Mps
connections equivalent to about 2000 traditional (28K) modems or 1000 high speed (56K)
modems.
Powerful Hardware
Service providers often have many powerful web servers that can be shared by several
companies. You can also expect them to have an effective load balancing, and necessary
backup servers.
Internet Service Providers are specialists on web hosting. Expect their servers to have more
than 99% up time, the latest software patches, and the best virus protection.
24-hour support
Make sure your Internet service provider offers 24-hours support. Toll-free phone could be
vital if you don't want to pay for long distance calls.
Daily Backup
Make sure your service provider runs a secure daily backup routine, otherwise you may lose
some valuable data.
Traffic Volume
Study the provider's traffic volume restrictions. Make sure that you don't have to pay a
fortune for unexpected high traffic if your web site becomes popular.
Study the provider's bandwidth and content restrictions. If you plan to publish pictures or
broadcast video or sound, make sure that you can.
Email Capabilities
Make sure your provider fully supports the email capabilities you need.
Database Access
Make sure your provider fully supports the database access you need if you plan to use
databases from your site
A domain name is a unique name for a web site, like microsoft.com and w3schools.com.
2
Domain names must be registered. When domain names are registered they are added to a
large domain name register, and information about your site - including your internet IP
address - is stored on a DNS server.
DNS stands for Domain Name System. A DNS server is responsible for informing all other
computers on the Internet about your domain name and your site address.
Domain Name Registration provides registration services for .com .net .org .biz .info .us
.nu .ws .cc and .tv domains.
Newer domain extensions such as .biz .info and .us have more choices available.
The shorter your domain, the easier it is to reach and the less are the chance the user will
make a typographical error while typing it.
Meaningful
Exposure - Search engines index your site and rank it for relevance against terms people
search for online. In order to maximize your sites exposure, consider including a relevant
search term in your domain.
Sub Domains
We use sub domains on a daily basis. The famous "www" of the World Wide Web is the most
common example of a sub domain. Sub domains can be created on a DNS server and they
don't need to be registered with a domain registrar, of course, the original domain would need
to be registered before a sub domain could be created. Common examples of sub domains
used on the internet are http://store.apple.com and http://support.microsoft.com.
Some providers will offer you a unique name under their own name like:
www.theircompany.com/yourcompany/
This is not a real domain name, it is a directory - and you should try to avoid it.
Expired Domains
When you register a domain, think of it as a rental. The result is that domains that were
previously registered regularly become available for registration again. You can see, and
search through a list of recently expired domains for free at http://www.dotdnr.com.
Hosting Capacities
3
How Much Disk Space?
A small or medium web site will need between 10 and 100MB of disk space.
Expect each HTML page to take up between 5 and 50KB of disk space on your web server,
depending on the use of images or other space consuming elements.
Make sure that you know your needs before you start looking for your web host.
Monthly Traffic
A small or medium web site will consume between 1GB and 5GB of data transfer per month.
You can calculate this by multiplying your average page size with the number of expected
page views per month. If your average page size is 30KB and you expect 50,000 page views
per month, you will need 0.03MB x 50,000 = 1.5GB.
Larger, commercial sites often consume more than 100GB of monthly traffic.
Before you sign a contract with a host provider, make sure to check this:
Connection Speed
Visitors to your web site will often connect via a modem, but your host provider should have a
much faster connection.
In the early days of the Internet a T1 connection was considered a fast connection. Today
connection speeds are much faster.
1 byte equals to 8 bits (and that's the number of bits used to transport one character). Low
speed communication modems can transport from about 14 000 to 56 000 bits per second (14
to 56 kilobits per second). That is somewhere between 2000 and 7000 characters per second,
or about 1 to 5 pages of written text.
One kilobit (Kb) is 1024 bits. One megabit (Mb) is 1024 kilobits. One gigabit (Gb) is 1024
megabits.
Speed per
Name Connection
second
T1 Digital 1.55Mb
4
Windows Hosting
Windows hosting means hosting of web services that runs on the Windows operating system.
You should choose Windows hosting if you plan to use ASP (Active Server Pages) as server
scripting, or if you plan to use a database like Microsoft Access or Microsoft SQL Server.
Unix Hosting
Unix hosting means hosting of web services that runs on the Unix operating system.
Unix was the first (original) web server operating system, and it is known for being reliable
and stable. Often less expensive than Windows.
Asp.net is a server-side scripting technology developed by Microsoft. With ASP you can create
dynamic web pages by putting script code inside your HTML pages. The code is executed by
the web server before the page is returned to the browser.
JSP
JSP is a server-side technology much like ASP developed by Sun. With JSP you can create
dynamic web pages by putting Java code inside your HTML pages. The code is executed by
the web server before the page is returned to the browser. Since JSP uses Java, the
technology is not restricted to any server-specific platform.
PHP
Just like ASP, PHP is a server-side scripting language which allows you to create dynamic web
pages by putting script code inside your HTML pages. The code is executed by the web server
before the page is returned to the browser.
5
Web Hosting Database Technologies
If your web site needs to update large quantities of information via the web, you will need a
database to store your information.
There are many different database systems available for web hosting. The most common are
MS Access, MySQL, SQL Server, and Oracle.
HTML
What is an HTML File?
HTML stands for Hyper Text Markup Language
An HTML file is a text file containing small markup tags called HTML Elements
The markup tags tell the Web browser how to display the page
<html>
<head>
<title> Title of page </title>
</head>
<body>
This is my first homepage. <b> Hello Welcome
in bold </b>
</body>
</html>
HTML Tags
HTML tags are used to mark-up HTML elements
HTML tags are surrounded by the two characters < and > called angle brackets
The first tag in a pair is the start tag, the second tag is the end tag
The text between the start and end tags is the element content
Example 1:
Example 2:
Character Entities
Some characters like the < character, have a special meaning in HTML, and therefore cannot
be used in the text. To display a less than sign (<) in HTML, we have to use a character
entity.
HTML Links
HTML uses a hyperlink to link to another document on the Web.
HTML uses the <a> (anchor) tag to create a link to another document.
An anchor can point to any resource on the Web: an HTML page, an image, a sound file, a
movie, etc.
7
The <a> tag is used to create an anchor to link from, the href attribute is used to address the
document to link to, and the words between the open and close of the anchor tag will be
displayed as a hyperlink.
With the target attribute, you can define where the linked document will be opened.
The line below will open the document in a new browser window:
The name attribute is used to create a named anchor. When using named anchors we can
create links that can jump directly into a specific section on a page, instead of letting the
user scroll around to find what he/she is looking for.
The name attribute is used to create a named anchor. The name of the anchor can be any
text you care to use.
You should notice that a named anchor is not displayed in a special way.
A hyperlink to the Engineering college from WITHIN the file will look like this:
Example :: Demo of jumping to another part of the document on the same page
<html>
<body>
8
<p> <a href="#C4">See also Chapter 4.</a> </p>
<h2>Chapter 1</h2>
<p>This chapter explains html</p>
<h2>Chapter 2</h2>
<p>This chapter explains web</p>
<h2>Chapter 3</h2>
<p>This chapter explains script</p>
<h2>Chapter 5</h2>
<p>This chapter explains java</p>
</body>
</html>
( disp.html )
<html>
<body>
<h1> ABC Company </h1>
<p> <a href=phone.html> display phone list </a> to get phone numbers of all
</p>
</body>
</html>
( phone.html )
<h1> <a name = “top”> Name of emp ( Mobile No) </a> </h1>
<p> <a href = “ # top”> Go back to beginning </a> of this page </p>
</body>
</html>
9
HTML Frames
With frames, one can display more than one HTML document in the same browser window.
Each HTML document is called a frame, and each frame is independent of the others.
The values of the rows/columns indicate the amount of screen area each row/column
will occupy
In the example below we have a frameset with two columns. The first column is set to 25% of the width of
the browser window. The second column is set to 75% of the width of the browser window. The HTML
document "frame_a.htm" is put into the first column, and the HTML document "frame_b.htm" is put into
the second column:
<frameset cols="25%,75%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
</frameset>
Example ::
<html>
<frameset cols="20%,80%">
<frame src="ind.htm" name = “a1”>
<frame src="disp.htm" name = “a2”>
</frameset>
</html>
( ind.htm )
<html>
<body>
<a href ="auinfo.htm" target = “a2”> AU </a>
<a href ="gitam.htm" target = “a2”> GITAM </a>
</body>
</html>
( disp.htm )
<html>
<body>
10
<center> click on links of left page </center>
</body>
</html>
( auinfo.htm )
<html>
<body>
AU information
</body>
</html>
( gitam.htm )
<html>
<body>
<center> Gitam Web page </center>
</body>
</html>
HTML Tables
With HTML one can create tables.
Tables
Tables are defined with the <table> tag. A table is divided into rows (with the <tr> tag), and each row is
divided into data cells (with the <td> tag). The letters td stands for "table data," which is the content of a
data cell. A data cell can contain text, images, lists, paragraphs, forms, horizontal rules, tables, etc.
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
To display a table with borders, you will have to use the border attribute:
<table border="1">
11
Headings in a Table
<table border="1">
<tr>
<th>Heading</th>
<th>Another Heading</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
</table>
Table cells with no content are not displayed very well in most browsers.
A non-breaking space ( ) is used to empty data cells, and to make the borders visible:
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td> </td>
</tr>
</table>
row 1, cell 1
Example ::
<html>
<body>
<table border="6">
<caption>My Caption</caption>
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
12
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
</body>
</html>
HTML Lists
HTML supports ordered, unordered and definition lists.
Unordered Lists
An unordered list is a list of items. The list items are marked with bullets (typically small black
circles).
An unordered list starts with the <ul> tag. Each list item starts with the <li> tag.
<ul>
<li>Coffee</li>
<li>Milk</li>
</ul>
Coffee
Milk
Inside a list item you can put paragraphs, line breaks, images, links, other lists, etc.
Ordered Lists
An ordered list is also a list of items. The list items are marked with numbers.
An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.
<ol>
<li>Coffee</li>
<li>Milk</li>
</ol>
13
Here is how it looks in a browser:
1. Coffee
2. Milk
Inside a list item you can put paragraphs, line breaks, images, links, other lists, etc.
Definition Lists
A definition list is not a list of items. This is a list of terms and explanation of the terms.
A definition list starts with the <dl> tag. Each definition-list term starts with the <dt> tag. Each definition-
list definition starts with the <dd> tag.
<dl>
<dt>Coffee</dt>
<dd> <i> Black hot drink </i> </dd>
<dt>Milk</dt>
<dd> <i> White cold drink </i> </dd>
</dl>
Coffee
Black hot drink
Milk
White cold drink
Inside a definition-list definition (the <dd> tag) you can put paragraphs, line breaks, images,
links, other lists, etc.
Example ::
<html>
<body>
</body>
</html>
An Ordered List:
1. Coffee
2. Tea
3. Milk
Example ::
<html>
14
<body>
<h4>A nested List:</h4>
<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>China</li>
<li>Africa</li>
</ul>
</li>
</ul>
</li>
<li>Milk</li>
</ul>
</body>
</html>
A nested List:
Coffee
Tea
o Black tea
o Green tea
China
Africa
Milk
Form elements are elements that allow the user to enter information (like text fields, textarea
fields, drop-down menus, radio buttons, checkboxes, etc.) in a form.
Input
The most used form tag is the <input> tag. The type of input is specified with the type
attribute. The most commonly used input types are explained below.
15
Text Fields
Text fields are used when you want the user to type letters, numbers, etc. in a
form.
<form>
First name: <input type="text" name="fname">
<br>
Last name: <input type="text" name="lname">
</form>
First name:
Last name:
Radio Buttons
Radio Buttons are used when you want the user to select one of a limited
number of choices.
<form>
<input type="radio" name="sex" value="male"> Male
<br>
<input type="radio" name="sex" value="female"> Female
</form>
Male
Female
Checkboxes
Checkboxes are used when you want the user to select one or more options of
a limited number of choices.
<form>
bike: <input type="checkbox" name="vehicle" value="B">
<br>
car: <input type="checkbox" name="vehicle" value="C">
<br>
airplane: <input type="checkbox" name="vehicle" value="Ap">
</form>
16
When the user clicks on the "Submit" button, the content of the form is sent
to another file. The form's action attribute defines the name of the file to send
the content to. The file defined in the action attribute usually does something
with the received input.
Submit
Username:
If you type some characters in the text field above, and click the "Submit" button, you will
send your input to a page called "html_form_action.asp". That page will show you the
received input.
Example ::
<html>
<body>
<form name="input" action="html_form_action.asp" method="get">
<p>
If you click the "Submit" button, you will send your input to a new page called
html_form_action.asp.
</p>
</body>
</html>
Example ::
<html>
<body>
<form action=mailto: [email protected] method="post" enctype="text/plain">
<h3>This form sends an e-mail to W3Schools.</h3>
Mail:<br>
17
<input type="text" name="mail" value="yourmail" size="20">
<br>
Comment:<br>
</form>
</body>
</html>
Mail:
yourmail
Comment:
yourcomment
Send Reset
HTML Images
The <img> tag is empty, which means that it contains attributes only and it has no closing
tag.
To display an image on a page, you need to use the src attribute. src stands for "source". The
value of the src attribute is the URL of the image you want to display on your page.
<img src="url">
The URL points to the location where the image is stored. For example an image named
"boat.gif" located in the directory "images" on "www.w3schools.com" has the URL:
http://www.w3schools.com/images/boat.gif.
The browser puts the image where the image tag occurs in the document. If you put an image
tag between two paragraphs, the browser shows the first paragraph, then the image, and
then the second paragraph.
18
The alt attribute is used to define an "alternate text" for an image. The value of the alt attribute is an
author-defined text:
The "alt" attribute tells the reader what he or she is missing on a page if the browser can't
load images. The browser will then display the alternate text instead of the image. It is a good
practice to include the "alt" attribute for each image on a page, to improve the display and
usefulness of your document for people who have text-only browsers.
Example ::
<html>
<body>
<p>
An image: <img src="sunset.jpg" width="144" height="50">
</p>
<p>
A moving image: <img src="hackanm.gif" width="48" height="48">
</p>
<p>
Note that the syntax of inserting a moving image is no different from that of a non-
moving image.
</p>
</body>
</html>
Example ::
<html>
<body background="background.jpg">
<h3>Look: A background image!</h3>
<p>Both gif and jpg files can be used as HTML backgrounds.</p>
<p>If the image is smaller than the page, the image will repeat itself.</p>
</body>
</html>
Example ::
<html>
<body>
<p>
<img src="hackanm.gif" width="20" height="20">
</p>
<p>
<img src="hackanm.gif" width="45" height="45">
</p>
<p>
<img src="hackanm.gif" width="70" height="70">
</p>
19
<p>
You can make a picture larger or smaller changing the values in the "height"
and "width" attributes of the img tag.
</p>
</body>
</html>
HTML Backgrounds
A good background can make a Web site look really great.
The <body> tag has two attributes where you can specify backgrounds. The background can
be a color or an image.
Bgcolor
<body bgcolor="#000000">
<body bgcolor="rgb(0,0,0)">
<body bgcolor="black">
Background
<body background="clouds.gif">
<body background="http://www.w3schools.com/clouds.gif">
The URL can be relative (as in the first line above) or absolute (as in the second line above).
Example ::
<html>
<body bgcolor="#d0d0d0">
</body>
</html>
Example ::
<html>
20
<body background="rock.jpg">
<h3>Image Background</h3>
<p>Both gif and jpg files can be used as HTML backgrounds.</p>
<p>If the image is smaller than the page, the image will repeat itself.</p>
</body>
</html>
HTML Colors
Colors are displayed combining RED, GREEN, and BLUE light sources.
HTML colors can be defined as a hexadecimal notation for the combination of Red, Green, and
Blue color values (RGB). The lowest value that can be given to one light source is 0 (hex #00)
and the highest value is 255 (hex #FF).
The table below shows the result of combining Red, Green, and Blue light
sources:.
Color
Color Color RGB
HEX
RED #FF0000rgb(255,0,0)
GREEN#00FF00rgb(0,255,0)
W3C has listed 16 color names The color names are: aqua, black, blue, fuchsia, gray, green,
lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow.
In HTML 4.0 all formatting can be removed from the HTML document and stored in a
separate style sheet. Thus it separates the presentation from the document structure.
XHTML is the "new" HTML. The most important thing you can do is to start writing valid HTML
4.01. Also start writing your tags in lower case. Always close your tag elements.
What Is XHTML?
XHTML stands for EXtensible HyperText Markup Language
XHTML is aimed to replace HTML
21
The Most Important Differences:
XHTML elements must be properly nested
XHTML elements must always be closed
This is correct:
<table width="100%">
This is correct:
<table width="100%">
This is wrong:
<input checked>
<input readonly>
<input disabled>
This is correct:
HTML 4.01 defines a name attribute for the elements a, applet, frame, , img, and map. In
XHTML the name attribute is deprecated. Use id instead.
This is wrong:
This is correct:
22
<img src="picture.gif" id ="picture1" />
To make your XHTML compatible with today's browsers, you should add an extra space before
the "/" symbol.
All XHTML documents must have a DOCTYPE declaration. The html, head and body elements
must be present, and the title must be present inside the head element.
<body> </body>
</html>
XHTML DTD
<!DOCTYPE> Is Mandatory
the DOCTYPE
the Head
the Body
<!DOCTYPE ...>
<html>
<head>
<title>... </title>
</head>
<body> ... </body>
</html>
The DOCTYPE declaration should always be the first line in an XHTML document.
An XHTML Example
23
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>simple document</title>
</head>
An XHTML document is validated against a Document Type Definition (DTD). Before an XHTML
file can be properly validated, a correct DTD must be added as the first line of the file.
The Strict DTD includes elements and attributes that have not been deprecated or do not
appear in framesets:
The Transitional DTD includes everything in the strict DTD plus deprecated elements and
attributes:
The Frameset DTD includes everything in the transitional DTD plus frames as well:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>simple document</title>
</head>
<body>
<p>a simple paragraph</p>
</body>
</html>
What is CSS?
CSS stands for Cascading Style Sheets
Styles define how to display HTML elements
24
Styles are normally stored in Style Sheets
Styles sheets define HOW HTML elements are to be displayed, just like the font tag and the
color attribute in HTML 3.2. Styles are normally saved in external .css files. External style
sheets enable you to change the appearance and layout of all the pages in your Web, just by
editing one single CSS document!
Style sheets allow style information to be specified in many ways. Styles can be specified
inside a single HTML element, inside the <head> element of an HTML page, or in an external
CSS file. Even multiple external style sheets can be referenced inside a single HTML
document.
Cascading Order
What style will be used when there is more than one style specified for an HTML element?
Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet
by the following rules, where number four has the highest priority:
1. Browser default
2. External style sheet
CSS Syntax
The CSS syntax is made up of three parts: a selector, a property and a value:
The selector is normally the HTML element/tag you wish to define, the
property is the attribute you wish to change, and each property can take a
value. The property and value are separated by a colon, and surrounded by
curly braces:
25
body {color: black}
Note: If the value is multiple words, put quotes around the value:
Note: If you wish to specify more than one property, you must separate each
property with a semicolon. The example below shows how to define a center
aligned paragraph, with a red text color:
p {text-align:center;color:red}
To make the style definitions more readable, you can describe one property on
each line, like this:
p
{
text-align: center;
color: black;
font-family: arial
}
Grouping
You can group selectors. Separate each selector with a comma. In the
example below we have grouped all the header elements. All header elements
will be displayed in green text color:
h1,h2,h3,h4,h5,h6
{
color: green
}
The class Selector
With the class selector you can define different styles for the same type of HTML element.
Say that you would like to have two types of paragraphs in your document:
one right-aligned paragraph, and one center-aligned paragraph. Here is how
you can do it with styles:
<p class="right">
This paragraph will be right-aligned.
</p>
<p class="center">
26
This paragraph will be center-aligned.
</p>
Note: To apply more than one class per given element, the syntax is:
The paragraph above will be styled by the class "center" AND the class "bold".
You can also omit the tag name in the selector to define a style that will be
used by all HTML elements that have a certain class. In the example below, all
HTML elements with class="center" will be center-aligned:
In the code below both the h1 element and the p element have
class="center". This means that both elements will follow the rules in the
".center" selector:
<h1 class="center">
This heading will be center-aligned
</h1>
<p class="center">
This paragraph will also be center-aligned.
</p>
Add Styles to Elements with Particular Attributes
You can also apply styles to HTML elements with particular attributes.
The style rule below will match all input elements that have a type attribute
with a value of "text":
You can also define styles for HTML elements with the id selector. The id selector is defined as
a #.
The style rule below will match the element that has an id attribute with a
value of "green":
The style rule below will match the p element that has an id with a value of
"para1":
p#para1
{
27
text-align: center;
color: red
}
When a browser reads a style sheet, it will format the document according to it. There are
three ways of inserting a style sheet:
An external style sheet is ideal, when the style is applied to many pages.
Each page
must link to the style sheet using the <link> tag. The <link> tag goes inside
the head section.
<head>
<link rel="stylesheet" type="text/css"
href="mystyle.css">
</head>
<head>
<style type="text/css">
body {background-color: red}
p {margin-left: 20px}
</style>
</head>
Inline Styles
An inline style should be used when a unique style is to be applied to a single occurrence of an
element.
To use inline styles you use the style attribute in the relevant tag. The style
attribute can contain any CSS property. The example shows how to change
the color and the left margin of a paragraph:
If some properties have been set for the same selector in different style sheets, the values will
be inherited from the more specific style sheet.
28
For example, an external style sheet has these properties for the h3 selector:
h3
{
color: red;
text-align: left;
font-size: 8pt
}
And an internal style sheet has these properties for the h3 selector:
h3
{
text-align: right;
font-size: 20pt
}
If the page with the internal style sheet also links to the external style sheet
the properties for h3 will be:
color: red;
text-align: right;
font-size: 20pt
The color is inherited from the external style sheet and the text-alignment and the font-size is
replaced by the internal style sheet.
<html>
<head>
<style type="text/css">
body {background-color: yellow}
h1 {background-color: #00ff00}
h2 {background-color: transparent}
p {background-color: rgb(250,0,255)}
</style>
</head>
<body>
<h1> Java made simple </h1>
<h2> Introduction </h2>
<p> Java was invented by SunMicro … </p>
</body>
</html>
<html>
<head>
<style type="text/css">
h1 {color: #00ff00}
h2 {color: #dda0dd}
p {color: rgb(0,0,255)}
29
</style>
</head>
<body>
</body>
<h1> Java made simple </h1>
<h2> Introduction </h2>
<p> Java was invented by SunMicro … </p>
</html>
<html>
<head>
<style type="text/css">
ul.disc {list-style-type: disc}
ul.circle {list-style-type: circle}
ul.square {list-style-type: square}
ul.none {list-style-type: none}
</style>
</head>
<body>
<ul class="disc">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="circle">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="square">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="none">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
30
JavaScript
JavaScript was designed to add interactivity to HTML pages
JavaScript is a scripting language
Java and JavaScript are two completely different languages in both concept and design!
To insert a JavaScript into an HTML page, we use the <script> tag. Inside the <script> tag we
use the "type=" attribute to define the scripting language.
The word document.write is a standard JavaScript command for writing output to a page.
Scripts in the head section: Scripts to be executed when they are called, or when an event
is triggered, go in the head section. When you place a script in the head section, you will
ensure that the script is loaded before anyone uses it.
Scripts in the body section: Scripts to be executed when the page loads go in the body
section. When you place a script in the body section it generates the content of the page.
Scripts in both the body and the head section: You can place an unlimited number of
scripts in your document, so you can have scripts in both the body and the head section.
Sometimes you might want to run the same JavaScript on several pages, without having to
write the same script on every page.
To simplify this, you can write a JavaScript in an external file. Save the external JavaScript file
with a .js file extension. The external script cannot contain the <script> tag!
To use the external script, point to the .js file in the "src" attribute of the <script> tag:
<html>
<head>
<script src="xxx.js"></script>
</head>
<body>
</body>
</html>
31
Declaring java Script Variables
var x;
var carname;
Like any other language, Java script has operators, control structures, with all object oriented
features
<html>
<body>
<script type="text/javascript">
switch (theDay)
{
case 5:
document.write("<b>Finally Friday</b>");
break;
case 6:
document.write("<b>Super Saturday</b>");
break;
default:
document.write("<b>I'm really looking forward to this weekend!</b>");
}
</script>
<p>This JavaScript will generate a different greeting based on what day it is</p>
</body>
</html>
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’);
32
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’);
33
Prompt Box
A prompt box is often used if you want the user to input a value before entering a page.
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");
JavaScript Functions
A function contains code that will be executed by an event or by a call to that function.
You may call a function from anywhere within the 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 the function is read/loaded by the browser
before it is called, it could be wise to put it in the <head> section.
<html>
<head>
<script type="text/javascript">
function dispMessage()
{
alert("Hello World!");
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onClick="dispMessage()" >
</form>
</body>
</html>
Events
By using JavaScript, we have the ability to create dynamic web pages. Events are actions that
can be detected by JavaScript.
Every element on a web page has certain events which can trigger JavaScript functions. For
example, we can use the onClick event of a button element to indicate that a function will run
when a user clicks on the button. We define the events in the HTML tags.
Examples of events:
34
A mouse click
A web page or an image loading
A keystroke
The onload and onUnload events are triggered when the user enters or leaves the page.
The onload event is often used to check the visitor's browser type and browser version, and
load the proper version of the web page based on the information.
Both the onload and onUnload events are also often used to deal with cookies that should be
set when a user enters or leaves a page.
The onFocus, onBlur and onChange events are often used in combination with validation of
form fields.
onSubmit
The onSubmit event is used to validate ALL form fields before submitting it.
Below lineis an example of how to use the onSubmit event. The checkForm()
function will be called when the user clicks the submit button in the form. If
the field values are not accepted, the submit should be cancelled. The function
checkForm() returns either true or false. If it returns true the form will be
submitted, otherwise the submit will be cancelled:
<a href="http://www.w3schools.com"
35
onmouseover="alert('An onMouseOver event');return false">
<img src="w3schools.gif" width="100" height="30">
</a>
The throw statement allows you to create an exception. If you use this statement together
with the try...catch statement, you can control program flow and generate accurate error
messages.
Syntax
throw(exception)
Example
The example below determines the value of a variable called x. If the value of
x is higher than 10 or lower than 0 we are going to throw an error. The error
is then caught by the catch argument and the proper error message is
displayed:
<html>
<body>
<script type="text/javascript">
var x=prompt("Enter a number between 0 and 10:","");
try
{
if(x>10)
throw "Err1";
else if(x<0)
throw "Err2";
}
catch(er)
{
if(er=="Err1")
alert("Error! The value is too high");
if(er == "Err2")
alert("Error! The value is too low");
}
</script>
</body>
</html>
36
Object Oriented Java script
JavaScript is an Object Oriented Programming (OOP) language. An OOP language allows you
to define your own objects and make your own variable types.
<html>
<body>
<script type="text/javascript">
var str="Hello world!";
</script>
</body>
</html>
<html>
<body>
<script type="text/javascript">
a[0] = "java";
a[1] = "C";
a[2] = "CPP";
document.write(a.concat(b));
</script>
</body>
</html>
37
Example :: Demo of Date objects
<html>
<body>
<script type="text/javascript">
var c = new Date;
with ( c )
{
document.write( “day:” + getDate() + “month:” + getMonth() + “year:” +
getYear() );
</script>
</body>
</html>
The JavaScript Navigator object contains all information about the visitor's browser. We are
going to look at two properties of the Navigator object:
Example
<html>
<body>
<script type="text/javascript">
</script>
</body>
</html>
38
What is a Cookie?
A cookie is a variable that is stored on the visitor's computer. Each time the same computer
requests a page with a browser, it will send the cookie too. With JavaScript, you can both
create and retrieve cookie values.
Examples of cookies:
Name cookie - The first time a visitor arrives to your web page, he or she must fill in
her/his name. The name is then stored in a cookie. Next time the visitor arrives at your
page, he or she could get a welcome message like "Welcome John Doe!" The name is
retrieved from the stored cookie
Password cookie - The first time a visitor arrives to your web page, he or she must fill
in a password. The password is then stored in a cookie. Next time the visitor arrives at
your page, the password is retrieved from the cookie
Date cookie - The first time a visitor arrives to your web page, the current date is
stored in a cookie. Next time the visitor arrives at your page, he or she could get a
message like "Your last visit was on Tuesday August 11, 2005!" The date is retrieved
from the stored cookie
In this example we will create a cookie that stores the name of a visitor. The first time a
visitor arrives to the web page, he or she will be asked to fill in her/his name. The name is
then stored in a cookie. The next time the visitor arrives at the same page, he or she will get
welcome message.
First, we create a function that stores the name of the visitor in a cookie variable:
The parameters of the function above hold the name of the cookie, the value of the cookie,
and the number of days until the cookie expires.
In the function above we first convert the number of days to a valid date, then we add the
number of days until the cookie should expire. After that we store the cookie name, cookie
value and the expiration date in the document.cookie object.
Then, we create another function that checks if the cookie has been set:
39
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
The function above first checks if a cookie is stored at all in the document.cookie object. If the
document.cookie object holds some cookies, then check to see if our specific cookie is stored.
If our cookie is found, then return the value, if not - return an empty string.
Last, we create the function that displays a welcome message if the cookie is set, and if the cookie is not
set it will display a prompt box, asking for the name of the user:
function checkCookie()
{
username=getCookie('username');
if (username!=null && username!="")
{
alert('Welcome again '+username+'!');
}
else
{
username=prompt('Please enter your name:',"");
<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
40
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
function checkCookie()
{
username=getCookie('username');
if (username!=null && username!="")
{
alert('Welcome again '+username+'!');
}
else
{
username=prompt('Please enter your name:',"");
if (username!=null && username!="")
{
setCookie('username',username,365);
}
}
}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>
The example above runs the checkCookie() function when the page loads.
<html>
<head>
<script type="text/javascript">
function timedMsg()
{
var t= setTimeout(" alert( '5 seconds ' ) " , 5000);
}
41
</script>
</head>
<body>
<form>
<input type="button" value= "Display timed alertbox!" onClick = "timedMsg()" >
</form>
<p>Click on the button above. An alert box will be displayed after 5 seconds.</p>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function validate()
{
var fage;
fage = document.biodata.age.value;
if (fage < 20 || fage > 58)
{
alert(‘age incorrect .. enter again’);
return false;
}
indin = document.biodata.mail.value.indexOf(“@”);
if (indin == -1)
{
alert("email incorrect .. enter again”);
return false;
}
return true;
</script>
</head>
<body>
</form>
42
</body>
</html>
<html>
<head> <title> order form </title>
function alerTv( )
{
s = document.tvform.tvsize.value;
alert(‘u wany a TV of size=’ + s);
}
function alerRef( )
{
s = document.reffrom.refcap.value;
alert(‘u wany a Refrig of size=’ + s);
}
</script>
</head>
<body>
<form name = “tvform” >
<h2> Television order </h2>
<hr> <br>
<hr>
</body>
</html>
43
<html>
<head> <title> Foreign Exchange converter </title> </head>
<body>
<script language= "javascript">
var f,a, val = 0, res = 0;
function convert(f,a)
{
val = parseFloat(f.rupees.value);
if ( a.match(“US”) )
{
res = val/40;
alert(‘in US dollars=’ + res);
}
if ( a.match(“SG”) )
{
res = val/28;
alert(‘in Singapore dollars=’ + res);
}
if ( a.match(“UK”) )
{
res = val/70;
alert(‘in UK pounds=’ + res);
}
}
</script>
</form>
</body>
</html>
<html>
<head> <title> Telephone directory </title></head>
<body>
44
var i,f,phone;
function getnumber(f )
{
phone = new Array(2567835, 2727311,2456563, 2345653,2897654,2342211);
i = f.nameBox.selectedIndex;
f.PhoneNumber.value = phone[i];
}
</script>
<pre> <center> <hr> <br> select name and pres button to get number
<br>
Event Handling :
45
EVENT TARGET CAUSE
<html>
<h4> Java Tutorial </h4>
<h5> Sample material is given for the students to learn </h5> <br> <br>
</body>
<html>
46
Example : Demo of Help Event
<html>
<h4> Java Tutorial </h4>
<h5> Sample material is given for the students to learn </h5> <br> <br>
<body>
Enter Name : <input type = “text” name = “vname” >
OnHelp = “ alert( ‘ Enter Name \n as per your SSC\n marks list ‘ )” >
</body>
<html>
<html>
<h4> Java Tutorial </h4>
<h5> Sample material is given for the students to learn </h5> <br> <br>
</body>
<html>
47
Example : Demo of KeyUp Event
<html>
<h4> Java Tutorial </h4>
<h5> Sample material is given for the students to learn </h5> <br> <br>
</body>
<html>
<html>
<head> <title> Telephone directory </title>
<script language= "javascript">
</head>
<body>
var i,f,phone;
function getnumber(f )
{
phone = new Array(2567835, 2727311,2456563, 2345653,2897654,2342211);
i = f.nameBox.selectedIndex;
f.PhoneNumber.value = phone[i];
}
</script>
</form>
</body>
</html>
48
Example : Demo Browser Object
<html>
<body>
<script type="text/javascript">
document.write("<p>Browser: ");
document.write(navigator.appName + "</p>");
document.write("<p>Browserversion: ");
document.write(navigator.appVersion + "</p>");
document.write("<p>Code: ");
document.write(navigator.appCodeName + "</p>");
document.write("<p>Platform: ");
document.write(navigator.platform + "</p>");
</script>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function pageload()
{
Location.href = ‘http://www.sun.com’ ;
}
</script>
</head>
<body>
<form>
<input type="button" value = "sunmicro" onClick = "pageload()" >
</form>
</body>
</html>
49
Example : Demo of onLoad, onUnLoad Event
<html>
<h4> Java Tutorial </h4>
<h5> Sample material is given for the students to learn </h5> <br> <br>
</body>
</html>
<html>
<h4> Andhra University </h4>
<h5> PG Studies </h5> <br> <br>
</body>
</html>
<html>
<head> <title> Demo of History Object </title>
function f1( )
{
k = history.length;
alert(“len=”+ k);
}
function f2( )
{
history.back;
}
function f3( )
{
history.forward;
50
}
function f4( )
{
history.go(-2);
}
</script>
</head>
<body>
<form >
</form>
</body>
</html>
<html>
<head> <title> IT calculation </title>
<script type="text/javascript">
function findIT(f)
{
anninc = parseFloat(f.ai.value);
ded = anninc/3;
if(totinc> 100000
tax = 29250 + 0.3*(totinc-100000);
f.tax.value = tax;
}
</script>
</head>
51
<body>
<form >
Enter Your annual Income <input type = “text” name = “ai” >
<br><hr>
<br><hr>
</form>
</body>
</html>
<html>
<head> <title> Room Reservation </title>
var f,a;
function check(f)
{
a = window.event.keyCode;
if( (!(a>=65 && a<= 90)) && ( !(a>=97 && a<= 122) ))
alert( “type name in characters” ) ;
function checknum(f)
{
a = window.event.keyCode;
}
</script>
</head>
<body>
<form >
52
Enter Name : <input type = “text” name = “namei”
onKeyPress = “check(this.form)” >
<br>
<br>
Type of Room :
</form>
</body>
</html>
53