HTML Notes (Autosaved) Final
HTML Notes (Autosaved) Final
http://www.echoecho.com/htmltables03.htm
Chapter 1
1
2
3
Chapter 2
Web programming
Web Programming refers to the writing, markup and coding involved in Web development,
which includes Web content, Web client and server scripting and network security.
4
• Perl
• Java Servlet
5
programming courses.
Another
decision is whether the
department wishes to use a
real language or a customised
teaching
language. Finally, the cost of
changing programming
languages must be considered
As noted earlier, many factors
must be considered prior to
beginning the language
selection process. Although
these factors eventually may
be included in the evaluation
criteria, they may also be used
to narrow the field of choices.
For example, the selection
6
will almost certainly be guided
by the methodology or
paradigm being taught and
the
number of languages to be
used throughout the series of
programming courses.
Another
decision is whether the
department wishes to use a
real language or a customised
teaching
language. Finally, the cost of
changing programming
languages must be considered
Cost of changing
Some educators naively think
that there is little cost in
7
changing programming
languages
because there are no serious
consequences if a decision
turns out badly. Lee and
Phillips
(2002) point out, however,
that considerable overhead
occurs in adopting a particular
language, including
preparation of lecture
materials, developing projects
and student
exercises, evaluating and
learning development
environments and installing
the chosen
8
language, requesting and
evaluating textbooks, and
training personnel. Such
overheads
indicate that care should be
taken when choosing a
language because that choice
is likely
to impact the educator for
several years.
1. Scalability
Scalability is the ability of a program to scale. For example, if you can do something on a
small database (say less than 1000 records), a program that is highly scalablewould work
well on a small set as well as working well on a large set (say millions, or billions of records)
2. Flexibility
3. Robustness
9
Robustness is the ability of a computer system to cope with errors during execution and
cope with erroneous input. Robustness can encompass many areas of computer science,
such as robust programming, robust machine learning, and Robust Security Network.
4. Exact requirements
5. Responsive
Responsive web design (RWD) is an approach to web design aimed at allowing desktop
webpages to be viewed in response to the size of the screen or web browser one is viewing
with.
6. Volume of data
7. Graphic content
8. Dynamic or static content
9. Cost
This translation is necessary because the server might be a UNIX machine while the visitor
might be sending information from a Windows platform. Windows and UNIX handle
information differently - so if there were no CGI, then UNIX machines could only communicate
with other UNIX machines etc. and that is pretty far from the basic idea of the world wide web.
10
Chapter 3
HTML Basics
Webpages are written in HTML - a simple scripting language.HTML is short
for HyperText Markup Language. A web page is a computer file, encoded in HyperText Markup
Language HTML and containing text, graphics files, and sound files, which are accessible
through the World Wide Web. Hypertext is simply a piece of text that works as a link.
MarkupLanguage is a way of writing layout information within documents.
Basically an HTML document is a plain text file that contains text and nothing else. When a
browser opens an HTML file, the browser will look for HTML codes in the text and use them to
change the layout, insert images, or create links to other pages.
Since HTML documents are just text files they can be written in even the simplest text editor.
A more popular choice is to use a special HTML editor - maybe even one that puts focus on the
visual result rather than the codes - a so-called WYSIWYG editor ("What You See Is What You
Get").
Some of the most popular HTML editors, such as FrontPage or Dreamweaver will let you
create pages more or less as you write documents in Word or whatever text editor you're using.
However, there are some very good reasons to create your own pages - or parts of them - by
hand...
You can read the code of other people's pages, and "borrow" the cool effects.
You can do the work yourself, when the editor simply refuses to create the effects you want.
You can use tags the editor does not support.
You can write your HTML by hand with almost any available text editor, including notepad,
WordPad that comes as a standard program with Windows.All you need to do is type in the code,
then save the document, making sure to put an .html extension or an .htm extension to the file
(for instance "mypage.html").
HTML Tags
Basically, a computer sees an "A" as simply an "A" - whether it is bold, italic, big or small.
To tell the browser that an "A" should be bold we need to put a markup in front of the A. Such a
markup is called a Tag.
11
All HTML tags are enclosed in angle brackets, that is < and >. For example, a piece of text as it
appears on the screen.
This is an example of bold text.
As you can see, the start tag <b> indicates that whatever follows should be written in bold. The
corresponding end tag </b> indicates that the browser should stop writing text in bold.
Page structure
All normal webpages consist of a head and a body.
Head
Body
The head is used for text and tags that do not show directly on the page.
The body is used for text and tags that are shown directly on the page.
Finally, all webpages have an <html> tag at the beginning and the end, telling the browser where
the document starts and where it stops. For example the most basic code - the code you will use
for any page you make, is shown below:
<html>
<head>
<!-- This section is for the title and technical info of the
page. -->
</head>
<body>
<!-- This section is for all that you want to show on the page.
-->
</body>
</html>
Head Section
The head section of the webpage includes all the stuff that does not show directly on the
resulting page.
The <title> and </title> tags encapsulate the title of your page. The title is what shows in the top
of your browser window when the page is loaded.
Another thing you will often see in the head section is metatags. Metatags are used for, among
other things, to improve the rankings in search engines.
Quite often the head section contains javascript which is a programming language for more
12
complex HTML pages.
Finally, more and more pages contain codes for cascading style sheets (CSS).
CSS is a rather new technique for optimizing the layout of major websites.
Since these aspects are way out of reach at this stage we will proceed with explaining the body
section.
Body section
The body of the document contains all that can be seen when the user loads the page. In the rest
of this tutorial you can learn in detail about all the different aspects of HTML, including:
Text
o Formatting
o Resizing
o Layout
o Listing
Inserting objects in HTML document
o Images
o Video clip
o Background music
Creating Hyperlinks
o Links to images
o To local pages
o To web pages at other sites
o To E-mails
o To Uniform Resources Locator
Inserting objects in HTML document
o Images
o Video clip
o Background music
Backgrounds
o Colors
o Images
o Hexadecimal Colors
Tables
o Rows
o Columns
o Table width and height
Designing a webpage with frames
o Frame set
o Frame column
o Frame rows
Metatags
13
Designing a webpage with forms
o Form input commands
o Text and numeric input commands
o Button commands
o Submit and reset commands
o
1. Text
Introduction
To enter text on your pages - all you have to do is simply enter the text.If you do not specify any
attributes for text it will use the default size, font etc. of the visitor's browser.
Browsers can only show fonts available on the visitor's PC.Therefore you are limited to using the
fonts that are available on almost any computer.If you need to use a fancy font, you should use
your graphics program to make an image with the text. This will assure that the visitor will see it
- even if he doesn't have the fancy font you're using.
Since images take up much more space than plain text, thus increasing download time, you
should use this option with care.
Base font
To specify the overall font for your page add the <basefont> tag at the beginning of
the <body> section.
<body>
<basefont face="arial, verdana, courier" size="4" color="green">
Hello! This is my page.<br>
All text looks the same<br>
since I only specified a basefont.<br>
</body>
</html>
The color attribute selects the desired color for your text. The face attribute selects
thedesired font.
Note:
14
If you enter a list of fonts, like in the example, the browser will use the first font in
the list available on the visitor's computer.The size attribute specifies the desired
size, between 1 (smallest) and 7 (biggest).
Font tag
The <font> tag will change the font.
<body>
<basefont color="green" face="arial" size="4">
Hello! This is my page.<br><br>
<font color="red" face="arial" size="2">
This local text looks different.
</font>
<br><br>
This text looks like the first line.
</body>
</html>
The color attribute selects the desired color for your text. The face attribute selects the
desired font.
Text Links
The tags used to produce links are the <a> and </a>.
The <a> tells where the link should start and the </a> indicates where the link ends.
Everything between these two will work as a link.
The target of the link is added to the <a> tag using
the href="http://www.whateverpage.com" setting.
The example below shows how to make the word here work as a link to yahoo.
15
Click <a href="http://www.yahoo.com">here</a> to go to yahoo.
You simply:
Font format
These are the tags for text formats:
<b>text</b> writes text as bold
<i>text</i> writes text in italics
<u>text</u> writes underlined text
<sub>text</sub> lowers text and makes it smaller
<sup> text
</sup> lifts text and makes it smaller
<blink>text</blink> guess yourself! (Note: Netscape only.)
<strike>text</strike> strikes a line through the text
<tt>text</tt> writes text as on a classic typewriter
<pre>text</pre> writes text exactly as it is, including spaces.
<em>text</em> usually makes text italic
<strong>text<strong> usually makes text bold
Font size
These are the tags for changing the font size.
16
<font size="7">
text </font>
writes text in biggest fontsize (36 pt)
The <small> and <big> tags are special in that they can be repeated. If you want to increase the
font size with a factor two, then you could do it like this:
Note:
While the font tag lets you specify font attributes in plain HTML, you really should look into the
tutorial on CSS to learn how to get full, flexible and much more advanced control of your text.
Text layout
These tags will let you control the layout.
HTML EXPLANATION
<p>text</p> Adds a paragraph break after the text.
(2 linebreaks).
<p align="left">text</p> Left justify text in paragraph.
<p align="center">text</p> Center text in paragraph.
<p align="right">text</p> Right justify text in paragraph.
text<br> Adds a single linebreak where the tag is.
<nobr>text</nobr> Turns off automatic linebreaks
- even if text is wider than the window.
Allows the browser to insert a linebreak
text<wbr> at exactly this point
- even if the text is within <nobr> tags.
<center>text</center> Center text.
<div align="center">text</div> Center text.
<div align="left">text</div> Left justify text.
<div align="right">text</div> Right justify text.
17
special tags <p align="left">special tags</p>
Note in particular the difference between the <p> and the <div> tags. The <div> tag allows
you to justify content without being forced to add a double linebreak.
Also, note that these alignment tags are not limited to text. They work on text, images,
applets or whatever it is that you insert on the page.
2. Lists
Introduction
To create a bulleted list you need to add a <ul> and a </ul> tag at the beginning and the end
of the list.
disc
18
circle
square
On the next page you can learn how to create and customize numbered lists....
Numbered list
This page shows how to make different kinds of numbered lists.
You have the following number options:
19
Plain numbers
Capital Letters
Small Letters
In addition to these options you can specify at which number the list should start. The default start
value for numbered lists is at number one (or the letter A).
20
a. This is one line
b. This is another line
c. And this is the final line
3. Links
Introduction
Links are the most fundamental part of the world wide web.
It is the links that tie it all together.
There are three different kinds of links you can have on your website:
21
Links to pages outside the current site (Global).
This section will cover the usual links: Texts and Images.
How to make a link
The tags used to produce links are the <a> and </a>.
The <a> tells where the link should start and the </a> indicates where the link ends.
Everything between these two will work as a link.
The target of the link is added to the <a> tag using
the href="http://www.whateverpage.com" setting.
The example below shows how to make the word here work as a link to yahoo.
Click <a href="http://www.yahoo.com">here</a> to go to yahoo.
You simply:
Specify the target in the <a href=" ">.
There are a few settings that can be useful for controlling the colors of text links.
22
Define colors for individual links on the page.
link - standard link - to a page the visitor hasn't been to yet. (standard color is blue -
#0000FF).
vlink - visited link - to a page the visitor has been to before. (standard color is purple -
#800080).
alink - active link - the color of the link when the mouse is on it. (standard color is red -
#FF0000).
Note
You can click here to learn more about the hexadecimal colorsystem that is used in HTML.
23
Placing font tags between the <a href> and the </a> tag.
This method will work on all browsers except MSIE 3.
Note:
It is important that both the <font> and the </font> tags are between the <a href> and </a> tags.
Note:
The RGB numbers indicate amounts of red, green, and blue using values between 0 and 255. You can
read more about converting between RGB colors and hexadecimal colors here.
Now, since neither of the two methods covers all browsers, we need to use both techniques at
once.
The last example is interesting. Not only because it will work on all browsers. But even more
because it shows a general approach to making your pages browser safe.
Since browsers simply leave out information that is not understood, you can work around
browser differences by simply adding different settings for multiple
Link Targets
By default, links will open in the current window or frame.
You need to add a target if you want the link to open in another window or frame than the link itself
24
is placed in.
_parent loads the page into the frame that is superior to the frame the hyperlink is in.
In addition to these, you can also enter the name of a frame window if your page is within a
frameset.
<head>
<title>This is my page</title>
<style type="text/css">
<!--
A{text-decoration:none}
-->
</style>
</head>
25
<body>
Welcome to my world!<br>
<a href="http://www.yahoo.com">This Link To Yahoo has no
underline</a>
</body>
</html>
Note:
The style setting will not cause an error if viewed on a browser that doesn't support it. The
browser will simply skip the effect - the link will look like an ordinary underlined link - but no
errors will occur.
The A:hover tells the browser that when the mouse is over a link the underline should appear.
The hover option only works on MSIE 4+.
(But it does not cause an error on Netscape if you include it - the effect just does not appear.).
<html>
<head>
<title>This is my page</title>
<style type="text/css">
<!--
A:link {text-decoration: none}
A:visited {text-decoration: none}
A:active {text-decoration: none}
A:hover {text-decoration: underline}
-->
</style>
</head>
<body>
Welcome to my world!<br>
<a href="http://www.yahoo.com">This Link To Yahoo has no
underline</a>
</body>
</html>
The methods described above will turn off the underline effect for links on the entire page.
26
If you want to turn off the effect for just a single link, add a style property to the <a href> tag:
NOTE:
The method described above only works on MSIE 3, Netscape 4 or newer browsers.
You can make multiple style settings instead of just removing the underline. Why not define the
colors you want for visited links as well?
<head>
<title>This is my page</title>
<STYLE TYPE="text/css"><!--
A.set1:link {color: #FF00FF; }
A.set1:active {color: #FFFF00; }
A.set1:visited {color: #00FFFF; }
</head>
<body>
Welcome to my world!<br>
<a href="http://www.yahoo.com CLASS=set1> Yahoo </a>
<a href="http://www.hotbot.com CLASS=set2> Hotbot </a>
</body>
</html>
Try clicking the two links below to see how these effects would work. (links have been deactivated
on this page)
Yahoo
27
Hotbot
Image links
If you want to make an image work as a link, the method is exactly the same as with texts.
You simply place the <a href> and the </a> tags on each side of the image.
Below is the HTML code used to make the image work as a link to a page called myfile.htm:
If you haven't entered a border setting you will see a small border around the image after turning it
into a link. To turn off this border, simply add border="0" to the <img> tag:
Images that work as links can show a popup text when you place the mouse over it.
This is done with the alt property in the <img> tag.
For example:
<a href="myfile.htm"><img src="rainbow.gif" border="0" alt="Link
to this page"></a>
Image mapping
It is possible to make one image link to several pages, depending on where the image is clicked.
This technique is called imagemapping.
28
You simply specify which areas of the image should link to where.
In the example below, if you position the mouse in the upper left corner it links to yahoo .... and in
the lower right corner.... it links to hotbot.
In the above example we only used rectangular imagemappings. Possible shapes are:
There are excellent tools out there to help you create imagemaps. No one calculates the
coordinates by hand.
If you want to use imagemaps on your site you will need to get a program that will allow you
to simply drag the mouse over the areas you want to work as links.
Below are links to websites that will help you create your imagemaps on the fly.
29
Just make sure that the image is uploaded to the web before using these tools.
Link within a page
Linking to anchors is very similar to normal links. Normal links always point to the top of a page.
Anchors point to a place within a page.
A # in front of a link location specifies that the link is pointing to an anchor on a page. (Anchor
meaning a specific place in the middle of your page).
<a name="chapter4"></a>
After doing this, you can make a link pointing to the anchor using the normal <a href> tag, like this:
Click <a href="#chapter4">here</a> to read chapter 4.
Note:
When linking to an anchor on a page you need to put a # in front of the anchor.
Link in Framesets
30
If a non-framebased HTML document contains a hyperlink that links to a page
called analysis.htm then it appears in the HTML document somewhat like this:
Now if the same link was in a frameset, (say in the frame window called menu) and we wanted it to
link to a page that is loaded in the other frame window, (named main) then the HTML code would
be:
We simply added the desired frame window (main) as a target for the link.
The link will be opened in the main frame window instead of the menu frame window
where the link itself is located.
Link to Email
Having a link that allows visitors to send email from your website can be a great addition to your
site, making it easy for your visitors to send questions or comments.
This will result in the visitor's email program opening a new email with your address already in the
To: field.
If you wish to have a specific subject in the email, you can add it to the html code
using subject= setting :
<a href="mailto:email@echoecho.com?subject=SweetWords">
Send Email</a>
31
Suppose you want an email link for your visitors containing specific text in the body of their
message, simply add &body=:
Or combine all the options and allow your visitor to send email with the address, subject and text
already entered.
<a href="mailto:email@echoecho.com?subject=SweetWords
&body=Please send me a copy of your new program!">Email Me</a>
If you want to customize the new window as to which buttons, menus etc. should be
available and which size it should have, you will need to do that with javascript.
You can use our Popup Tool to create your own customized windows without knowledge of
javascript.
4. Images
Introduction
This section will show how to add images to your pages.
We will start out with a presentation of the two main image types on webpages: jpg and gif.
32
After that, we will proceed with various ways to insert and customize images, with a special focus
on the different alignments you can choose.
While the focus of this section is images, there are a couple of aspects related to images that are
not covered here.
If you want to learn how to make images work as links, you should go to the links section.
If you want to learn about browser safe colors for images, you should go to the hexcolor
section.
If you want to learn about mouseover effects on images, you should go to the javascript
section.
GIF JPG
256 colors Unlimited colors
Can handle transparent areas Can't handle transparent areas
This format is not good at compressing Excellent for compressing photographs
photographs and complex images
In general, it is excellent for banners, In general, it is not good for banners,
buttons and clipart buttons and clipart.
Banners, buttons, dividers, clipart and other simple images usually work best as GIF's.
33
Photographs and other complex images usually work best as JPG's.
If you want to use an image that is in a format other than JPG or GIF, you will need to load
the image into a graphics program and save it as either JPG or GIF.
Inserting in HTML
Here is the HTML code used to insert the image on this webpage:
<img src="http://www.echoecho.com/rainbow.gif">
If the image is stored in the same folder as the HTML page, you can leave out the domain reference
(http://www.echoecho.com/) and simply insert the image with this code:
<img src="rainbow.gif">
On the following pages we will discuss different ways to control how your image is inserted.
Resizing
You can change the size of an image using the width and height attributes.
In general, it is not advisable to reduce image size using these settings, since the image will be
transferred over the internet in its original size no matter what reduction is set for it. This will slow the
loading of your webpage.
This means, that if you have an image that is bigger in size than you want it to be on your page, you
should reduce the size in a graphics program, rather than reducing the size on the webpage using
the width and height attributes.
On the contrary, sometimes, it can be wise to enlarge images using this technique.
Below are two presentations of the exact same image - with different settings for width and height.
34
<img src="http://www.echoecho.com/rainbow.gif" width="60"
height="60">
If you leave out the settings for width and height, the browser will automatically use the real size of
the image.
However, you should always enter the settings for width and height, even when using the real size!
The reason is that if the settings are left out, the browser can't build the page until the image is
loaded entirely.
This means, that the visitor cannot read text around the image while the image itself is loading -
which in turn will give the visitor an impression of a slow loading page.
Border around
You can add a border to the image using the border setting shown in the example below:
Note:
Netscape browsers will only show the border if the image is a link.
35
<img src="http://www.echoecho.com/rainbow.gif" border="5">
Adding a border to your image might help the visitor recognize that the image is a link.
However, the net is filled with images that work as links and have no borders indicating it - so
the average visitor is used to letting the mouse run over images to see if they are links.
Still - if you have an image that is often mistaken you might consider adding a border to it -
although you should probably consider changing the image entirely - since if it does not
indicate by itself that it is a link then it isn't serving it's purpose.
Alternative text
Spacing around
You can easily add space over and under your images
with the Vspace attribute.
In a similar way you can add space to the left and right of the image using the Hspace attribute.
Below is an example using these attributes:
As you see these settings allow you to add spacing around your image. Unfortunately, they also
force you to add the same spacing to each side of the image (over and under - or left and right).
The workaround for this, if you only want spacing on one side of the image is to use a 1x1 pixel
transparent gif image.
If, for example, you wanted a 10 pixel spacing to the left of your image you could use the
transparent image (pixel.gif) this way:
36
<img src="pixel.gif" width="10" height="1"><img
src="rainbow.gif">
The 1x1 pixel transparent gif image is simply stretched to whatever size you want the
spacing to have.
This 1x1 pixel "cowboy-trick" is probably one of the most widely used workarounds on the
entire net.
The reasons are obvious: It works on all browsers and it gives you complete pixel precision
in your design!
Alignment
You can align images according to the text around it, using the following alignments:
default aligns the image using the default settings of the Web browser. Same as baseline.
left aligns the image in the left margin and wraps the text that follows the image.
right aligns the image in the right margin and wraps the text that precedes the image.
top aligns the top of the image with the surrounding text.
texttop aligns the top of the image with the top of the tallest text in the line.
middle aligns the middle of the image with the surrounding text.
37
absmiddle aligns the image with the middle of the current line.
baseline aligns the image with the baseline of the current line.
bottom aligns the bottom of the image with the surrounding text.
absbottom aligns the image with the bottom of the current line.
center aligns the center of the image with the surrounding text.
In the table below you can see examples of the different vertical alignments you can make for an
image.
The note in the examples is only there to show how the circular sign is affected by other
images on the same line.
This means, that the alignments shown in the example are made to the circular sign and not the note.
HTML EXAMPLE
38
bla bla bla bla
Text wrapping
In addition to the vertical alignments covered on the previous page, images can also be aligned
horizontally.
Another way to obtain the same effect would be to enter the image and text in an invisible
table. Entering text in one column and the image in another would create a similar effect.
6. TABLES
6.1 Introduction
The HTML tables allow web authors to arrange data like text, images, links, other tables, etc.
into rows and columns of cells.Tables are used on websites for two major purposes:
The obvious purpose of arranging information in a table
The less obvious - but more widely used - purpose of creating a page layout with the use
of hidden tables.
Using tables to divide the page into different sections is an extremely powerful tool.
Almost all major sites on the web are using invisible tables to layout the pages.
39
The most important layout aspects that can be done with tables are:
A simple way to allow text to be written in two or more columns next to each other.
The importance of using tables for these layout purposes can't be overrated. However
there are a few things to keep in mind when doing so.
Most important is, that the content of a table is not shown until the entire table is loaded.
If you have extremely long pages, you should divide it into two or more tables - allowing
the user to start reading the upper content while the rest of the page is loading.
Solution
<html>
<head>
<title>HTML Tables</title>
</head>
<body>
40
<table border="1">
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
</body>
</html>
Table heading can be defined using <th> tag. This tag will be put to replace <td> tag,
which is used to represent actual data cell. Normally you will put your top row as table
heading as shown below, otherwise you can use <th> element in any row.
Here border is an attribute of <table> tag and it is used to put a border across all the cells. If
you do not need a border then you can use border="0".
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Header</title>
</head>
<body>
<tableborder="1">
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ramesh Raman</td>
<td>5000</td>
41
</tr>
<tr>
<td>Shabbir Hussein</td>
<td>7000</td>
</tr>
</table>
</body>
</html>
Write a HTML code that will display the following table when run on a web
browser.
Name Salary
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Colspan/Rowspan</title>
</head>
<body>
<tableborder="1">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
42
</tr>
<tr>
<tdrowspan="2">Row 1 Cell 1</td>
<td>Row 1 Cell 2</td>
<td>Row 1 Cell 3</td>
</tr>
<tr>
<td>Row 2 Cell 2</td>
<td>Row 2 Cell 3</td>
</tr>
<tr>
<tdcolspan="3">Row 3 Cell 1</td>
</tr>
</table>
</body>
</html>
Row 1 Cell
Row 1 Cell 3
2
Row 1 Cell 1
Row 2 Cell
Row 2 Cell 3
2
Row 3 Cell 1
Exercise
Write a HTML code that would be used to display the following table on a webpage when run on
a browser.
Products in a supermarket
Name Cost Date
Highly Perishable Oranges 100/kg 16/2/19
Bananas 120/kg 19/2/19
Perishable Colgate 200/= N/A
Close up 210/= N/A
43
Aqua fresh 220/= N/A
Goods once sold not returned
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Cellpadding</title>
</head>
<body>
<tableborder="1"cellpadding="5"cellspacing="5">
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ramesh Raman</td>
<td>5000</td>
</tr>
<tr>
<td>Shabbir Hussein</td>
<td>7000</td>
</tr>
</table>
</body>
</html>
Name Salary
44
Ramesh Raman 5000
PROPERTY DESCRIPTION
align=
left aligns content to the left of cells
right aligns content to the right of cells
center aligns content to the center of the cells
background=filename sets a background image for the cells
bgcolor=#rrggbb sets a background color for the cells
bordercolor=#rrggbb sets color for the border of cells
bordercolordark=#rrggbb sets color for the border shadow of cells
valign=
top aligns to the top of cells
middle aligns to the middle of the cells
bottom aligns to the bottom of cells
width=
n specify a minimum width for the cells in pixels
n% specify a minimum width for the cells in percent of
the table width
height=
n minimum height of cells in pixels
n% minimum height of cells in percentage of table height
ROPERTY DESCRIPTION
align=
left aligns content to the left of cells
right aligns content to the right of cells
center aligns content to the center of the cells
background=filename sets a background image for the cells
bgcolor=#rrggbb sets a background color for the cells
bordercolor=#rrggbb sets color for the border of cells
bordercolordark=#rrggbb sets color for the border shadow of cells
valign=
top aligns to the top of cells
middle aligns to the middle of the cells
bottom aligns to the bottom of cells
width=
n specify a minimum width for the cells in pixels
n% specify a minimum width for the cells in percent of
the table width
height=
n minimum height of cells in pixels
n% minimum height of cells in percentage of table height
45
These settings are only valid for <td> tags.
PROPERTY DESCRIPTION
colspan=n number of columns a cell should span
nowrap protects against linebreaks, even though the content
of a cell might be wider than the browser window
rowspan=n number of rows a cell should span
Assignment
2025CSCS B/C Out of Classwork
(a) Write a HTML code that would be used to generate the following output. (10 marks)
Fees for
Fee per Fee per single
Course Duration Revision dates
semester subject subject for
revision
DICT 6 months 10,500 1,000 12/08/2014- 2,000
DSS 6 months 10,500 1,000 12/09/2014 2,000
CIT 6 months 9,500 900 1,500
15/09/2014-
CEET 6 months 9,50046 900 1,500
01/10/2012
CCAT 6 months 9,500 900 1,500
All fees is payable through the college account
(b) Change the border colors to red. (5 marks)
(c) Change background color of the table to green. (5 marks)
(d) Insert a graphic of your choice as a background. (5 marks)
47
7. COLOURS
7.1 Introduction
By default, your webpage background is white in color. It may not be pleasant to the
programmer, but this can be changed. HTML provides the following two ways to decorate a
webpage background.
The name of the colour can be included in the code for example: Red, Blue, Red, Orange,
Lime among others.
(b) Format 1 - Use hex value
<tablebgcolor="#0000ff">
<tablebgcolor="rgb(0,0,120)">
An RGB color value is specified with: rgb(red, green, blue). Each parameter (red, green,
and blue) defines the intensity of the color and can be an integer between 0 and 255 or a
percentage value (from 0% to 100%).
For example, the rgb(0,0,255) value is rendered as blue, because the blue parameter is set
to its highest value (255) and the others are set to 0.
Also, the following values define equal color: rgb(0,0,255) and rgb(0%,0%,100%).
48
Example 7.1
Write a HTML code that would display a table with different colors as shown in figure 6.1 when
run on a browser.
Solution
<html>
<head>
<title>HTML Background Colors</title>
</head>
<body>
<!-- Format 1 - Use color name -->
<tablebgcolor="yellow"width="100%">
<tr><td>
This background is yellow
</td></tr>
</table>
</body>
</html>
Note: The background attribute is deprecated and it is recommended to use Style Sheet for
background setting.
<tagnamebackground="Image URL"...>
49
The most frequently used image formats are JPEG, GIF and PNG images.
Example
Here are the examples to set background images of a table.
<!DOCTYPE html>
<html>
<head>
<title>HTML Background Images</title>
</head>
<body>
</body>
</html>
The background image will scroll when the user scrolls down the page, unless
you have set it to be fixed. The following is the syntax for creating a fixed
image.
50
<body background="drkrainbow.gif" bgproperties="fixed">
By adding the bgproperties="fixed" you force the browser to let the background
be fixed even if the user is scrolling down the page.
Exercise
8. Frames
8.1 Introduction
HTML frames are used to divide a browser window into multiple sections where each section
can load a separate HTML document. A collection of frames in the browser window is known
as a frameset. The window is divided into frames in a similar way the tables are organized: into
rows and columns.
Disadvantages of Frames
There are few drawbacks with using frames, so it's never recommended to use frames in your
webpages:
Some smaller devices cannot cope with frames often because their screen is not big
enough to be divided up.
Sometimes your page will be displayed differently on different computers due to different
screen resolution.
The browser's back button might not work as the user hopes.
There are still few browsers that do not support frame technology.
Example
Following is the example to create three horizontal frames:
<!DOCTYPE html>
<html>
<head>
<title>HTML Frames</title>
</head>
<framesetrows="10%,80%,10%">
<framename="top"src="/html/top_frame.htm"/>
<framename="main"src="/html/main_frame.htm"/>
51
<framename="bottom"src="/html/bottom_frame.htm"/>
<noframes>
<body>
Your browser does not support frames.
</body>+
</noframes>
</frameset>
</html>
Example 8.1
Let's put above example as follows, here we replaced rows attribute by cols and changed their
width. This will create all the three frames vertically:
<!DOCTYPE html>
<html>
<head>
<title>HTML Frames</title>
</head>
<framesetcols="25%,50%,25%">
<framename="left"src="/html/top_frame.htm"/>
<framename="center"src="/html/main_frame.htm"/>
<framename="right"src="/html/bottom_frame.htm"/>
<noframes>
52
<body>
Your browser does not support frames.
</body>
</noframes>
</frameset>
</html>
Attribute Description
Cols specifies how many columns are contained in the frameset and the
size of each column. You can specify the width of each column in
one of four ways:
53
Rows This attribute works just like the cols attribute and takes the same
values, but it is used to specify the rows in the frameset. For
example to create two horizontal frames, use rows="10%, 90%".
You can specify the height of each row in the same way as
explained above for columns.
Border This attribute specifies the width of the border of each frame in
pixels. For example border="5". A value of zero means no border.
Attribute Description
Src This attribute is used to give the file name that should be loaded in the
frame. Its value can be any URL. For example, src="/html/top_frame.htm"
will load an HTML file available in html directory.
Name This attribute allows you to give a name to a frame. It is used to indicate
which frame a document should be loaded into. This is especially important
when you want to create links in one frame that load pages into an another
frame, in which case the second frame needs a name to identify itself as the
target of the link.
frameborder This attribute specifies whether or not the borders of that frame are shown;
it overrides the value given in the frameborder attribute on the <frameset>
tag if one is given, and this can take values either 1 (yes) or 0 (no).
marginwidth This attribute allows you to specify the width of the space between the left
54
and right of the frame's borders and the frame's content. The value is given
in pixels. For example marginwidth="10".
marginheight This attribute allows you to specify the height of the space between the top
and bottom of the frame's borders and its contents. The value is given in
pixels. For example marginheight="10".
Noresize By default you can resize any frame by clicking and dragging on the
borders of a frame. The noresize attribute prevents a user from being able
to resize the frame. For example noresize="noresize".
Scrolling This attribute controls the appearance of the scrollbars that appear on the
frame. This takes values either "yes", "no" or "auto". For example
scrolling="no" means it should not have scroll bars.
Longdesc This attribute allows you to provide a link to another page containing a long
description of the contents of the frame. For example
longdesc="framedescription.htm"
Example 8.2
<html>
<frameset cols="50%,50%">
<frame src="micheal.gif" scrolling="yes">
<frame src="html.gif">
</frameset>
</html>
55
8.3 Default pages
Default pages are the pages that will be loaded when the frameset is opened the first
time.Furthermore, we can add names to each frame window using the name setting.This will
allow us to make a link in one frame window, open a page in another frame window. In this
example we added names and default pages to the frame windows:
m main
e
n
u
We still have the screen divided in two columns, the left being 120 pixels the right using the rest
of the screen. (some screens are set to 640 pixels across, some to 800 and some to 1024, thats
why the * is needed).
But now we also have told the browser that the left frame window should load an HTML page
called menu.htm and that the right window should load an HTML document called frontf.htm.
In addition we have assigned the names menu and main to the two frame windows, so now we're
even able to link to specific windows.
56
We called the frame windows menu and main, but you could name them whatever you pleased.
The frameset with a menu window to the left and a main window to the right is the most
common frameset seen on the web.
There are a few more settings we could add to the frameset.
For instance you might want the frame borders to be invisible.
Proceed to the next page to learn how....
Now let's proceed with even more parameters for our frameset...
8.5 Scrollbars
Syntax
<frame scrolling="auto|yes|no">
Attribute Values
Value Description
Yes Scrollbars are always shown (even if they are not needed)
Examples 1
<frameset rows="16%,84%">
57
<frame src="top.htm" name="top">
<frame src="bottom.htm" name="bottom">
</frameset>
Examples 2
tl tr
bottom
<frameset rows="16%,84%">
<frameset cols="50%,50%">
<frame src="tl.htm" name="tl">
<frame src="tr.htm" name="tr">
</frameset>
<frame src="bottom.htm" name="bottom">
</frameset>
Examples 3
top
left right
<frameset rows="16%,84%">
<frame src="top.htm" name="top">
<frameset cols="50%,50%">
<frame src="left.htm" name="left">
<frame src="right.htm" name="right">
</frameset>
</frameset>
topleft topright
58
botleft botright
topleft topright
brtl brtr
botleft botrbot
topleft topright
botleft botright
59
topleft topright
botleft botright
Exercise 4.
Write a html code that would be used to display the following frameset browser window when
run.
20% 60% 20%
15%
10%
80%
70% 10%
15%
Example
Write a HTML code that would display the following Window when run on a browser.
60
Forms
Introduction
A form is simply an area that can contain form fields. Form fields are objects that allow the
visitor to enter information - for example text boxes, drop-down menus or radio buttons. When
the visitor clicks a submit button, the content of the form is usually sent to a program that runs on
the server. However, there are exceptions. JavaScript is sometimes used to create magic with
form fields. An example could be when turning options in a drop-down menu into normal links.
Examples of Application of forms
SEARCH!
61
FREE WEB-EMAIL AT
ECHOECHO.COM
Username:
Password:
Log In
Both examples send the contents of the form fields to programs running on the server.
CGI Scripts
When your form is submitted you need a program that can receive the information and do
something with it.Such programs are sometimes referred to as: CGI programs.CGI stands for
Common Gateway Interface, which is computer latin for a program that translates information.
This translation is necessary because the server might be a UNIX machine while the visitor
might be sending information from a Windows platform. Windows and UNIX handle
information differently - so if there were no CGI, then UNIX machines could only communicate
with other UNIX machines etc. and that is pretty far from the basic idea of the world wide web.
Form Tag
When a form is submitted, all fields on the form are being sent. The <form> tag tells the browser
where the form starts and ends. You can add all kinds of HTML tags between
the <form> and </form> tags.
This means that a form can easily include a table or an image along with the form fields
mentioned.
Syntax
62
<html>
<head>
<title>My Page</title>
</head>
<body>
<!-- Here goes HTML -->
<form>
<!-- Here goes form fields and HTML -->
</form>
<!-- Here goes HTML -->
</body>
</html>
Note:
Unlike a table, forms are not visible on the page.
To let the browser know where to send the content we add these properties to the <form> tag:
action=address
method=post or method=get
The address is the URL of the CGI script the content should be sent to.
The postand get methods are simply two different methods for submitting data to the script.
In the description of the script you are using it will be made clear whether the scripts should be
addressed using one method or the other.
Below is an example of a typical form tag, with both action and method specified.
<html>
<head>
<title>My Page</title>
</head>
<body>
<!-- Here goes HTML -->
<form method="post"
action="http://www.echoecho.com/cgi-bin/formmail.cgi">
<!-- Here goes form fields and HTML -->
</form>
<!-- Here goes HTML -->
</body>
63
</html>
Form Fields
These fields can be added to your forms:
1. Text
Text fields are one line areas that allow the user to input text.
SETTINGS:
The table below shows a listing of valid settings for text fields:
The size option defines the width of the field. That is how many visible characters it can contain.
The maxlength option defines the maximum length of the field. That is how many characters
can be entered in the field.
If you do not specify a maxlength, the visitor can easily enter more characters than are visible in
the field at one time.
The name setting adds an internal name to the field so the program that handles the form can
identify the fields.
The value setting defines what will appear in the box as the default value.
EXAMPLE 2:
64
</head>
<body>
<form name="myform">
<div align="center">
<br><br>
<input type="text" size="25" value="Enter your name here!">
<br><br>
</div>
</form>
</body>
</html>
2. Text areas
Unlike most other form fields, text areas are not defined with an <input> tag.
Instead you enter a <textarea> tag where you want the text area to start and a
closing </textarea> tag where you want the area to end.
Everything written between these tags will be presented in the text area box.
SETTINGS:
wrap=
off Turns off linebreaking
virtual Shows linebreaking, but
sends text as entered.
physical
Inserts linebreaks when
needed and even sends it.
The cols and rows settings are straightforward and simple. They specify how many columns and
rows you want in your text area.
The name setting adds an internal name to the field so the program that handles the form can
65
identify the fields.
The tabindex setting defines in which order the different fields should be activated when the
visitor clicks the tab key.
The wrap options are the most tricky part of text areas.
If you turn wrap off the text is handled as one long sequence of text without linebreaks.
If you set it to virtual the text appears on your page as if it recognized linebreaks - but when the
form is submitted the linebreaks are turned off.
If you set it to physical the text is submitted exactly as it appears on the screen - linebreaks
included.
AN EXAMPLE:
Look at this HTML example:
<html>
<head>
<title>My Page</title>
</head>
<body>
<form name="myform">
<div align="center">
This is outside the area<br><br>
<textarea cols="40" rows="5" name="myname">
Now we are inside the area - which is nice.
</textarea>
<br><br>
And now we are outside the area again.
</div>
</form>
</body>
</html>
3. Password
Password fields are similar to text fields.
The difference is that what is entered into a password field shows up as dots on the screen.
66
This is, of course, to prevent others from reading the password on the screen.
SETTINGS:
The size option defines the width of the field. That is how many visible characters it can
contain.
The maxlength option defines the maximum length of the field. That is how many
characters can be entered in the field.
If you do not specify a maxlength, the visitor can easily enter more characters than are
visible in the field at one time.
The name setting adds an internal name to the field so the program that handles the form
can identify the fields.
The value setting defines what will appear in the box as the default value.
The align setting defines how the field is aligned.
Valid entries are: TOP, MIDDLE, BOTTOM, RIGHT, LEFT, TEXTTOP, BASELINE,
ABSMIDDLE, ABSBOTTOM. The alignments are explained in the image section. You can
learn about the different alignments here.
The tabindex setting defines in which order the different fields should be activated when
the visitor clicks the tab key.
EXAMPLE:
67
<div align="center">
Enter Password : <input type="password" size="25">
<br><br>
</div>
</form>
</body>
</html>
5.
6. Check boxes
Check boxes are used when you want to let the visitor select one or more options from a
set of alternatives. If only one option is to be selected at a time you should use radio
buttons instead.
SETTINGS:
The name setting adds an internal name to the field so the program that handles the form
can identify the fields.
The tabindex setting defines in which order the different fields should be activated when
the visitor clicks the tab key.
EXAMPLE:
<html>
<head>
<title>My Page</title>
68
</head>
<body>
<form name="myform"
action="http://www.mydomain.com/myformhandler.cgi" method="POST">
<div align="center"><br>
<input type="checkbox" name="option1" value="Milk"> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked>
Butter<br>
<input type="checkbox" name="option3" value="Cheese"> Cheese<br>
<br>
</div>
</form>
</body>
</html>
Milk
Butter
Cheese
9. Radio Buttons
Radio buttons are used when you want to let the visitor select one - and just one - option
from a set of alternatives. If more options are to be allowed at the same time you should
use check boxes instead.
SETTINGS:
The name setting tells which group of radio buttons the field belongs to. When you select
one button, all other buttons in the same group are unselected.
If you couldn't define which group the current button belongs to, you could only have one
group of radio buttons on each page.
69
ABSMIDDLE, ABSBOTTOM.
The alignments are explained in the image section. You can learn about the different
alignments here.
The tabindex setting defines in which order the different fields should be activated when
the visitor clicks the tab key.
AN EXAMPLE:
Milk
Butter
Cheese
Water
Yoghurt
Sweets
70
Drop-down menus are probably the most flexible objects you can add to your forms.
Depending on your settings, drop-down menus can serve the same purpose as radio
buttons (one selection only) or check boxes (multiple selections allowed).
The advantage of a drop-down menu, compared to radio buttons or check boxes, is that it
takes up less space.But that is also a disadvantage, because people can't see all options in
the menu right away.
There is a workaround for this - with the size setting, you can customize the menu so it
shows more than just one option at a time, but when you do that - you also lose the
advantage of taking up less space.
Sometimes you may want to replace text fields with drop-down menus. This might be
because selecting from a menu is easier than typing. But it could also be because the
script that handles the form can't interpret just any text entry.
For example, you will often be asked to choose your county from a drop-down menu.
This might be because picking it from the menu is easier than typing the name of the
state.
Along the same line, you may often asked to enter the 2 letter initials of your state from a
drop-down menu as well.This could prevent confusion for the script that handles the form
input. If, say, the script was programmed to only accept capital letters, then a drop-down
menu would secure that no invalid entries were made.
SETTINGS:
Below is a listing of valid settings for drop-down menus:
HTML EXPLANATION EXAMPLE
select Drop-down menu
name= Name of the field.
size= Visible items in list.
multiple= Allows multiple choices if yes.
71
Drop-down menus combine <select> and <option>.
Both tags have an opening and a closing tag.
The name setting adds an internal name to the field so the program that handles the form can
identify the fields.
The size option defines how many items should be visible at a time. Default is one item.
The value setting defines what will be submitted if the item is selected. This is not
always the same as what it says in the menu. If our field was defined this way:
<option value="ID">Idaho</option>
then, in the menu it would say "Idaho" but when the form was submitted the abbreviated
"ID" would actually be sent.
You can force an item to be default selected by adding the selected option: <option
selected>
AN EXAMPLE:
72
</select>
</div>
</form>
</body>
</html>
Fresh Milk
Since visitors aren't always perfectionists you might consider adding a javascript
validation of the content before it is actually sent.
SETTINGS:
Below is a listing of valid settings for submit buttons:
The name setting adds an internal name to the button so the program that handles the
form doesn't confuse the button with the other fields.
The tabindex setting defines in which order the different fields should be activated when
the visitor clicks the tab key.
AN EXAMPLE:
73
Look at this HTML example:
<html>
<head>
<title>My Page</title>
</head>
<body>
<form name="myform" >
<div align="center">
<br><br>
<input type="text" size="25" value="Enter your name here!">
<br><input type="submit" value="Submit"><br>
</div>
</form>
</body>
</html>
15.
16. Reset button.
When a visitor clicks a reset button, the entries are reset to the default values.
SETTINGS:
The name setting adds an internal name to the button so the program that handles the form
doesn't confuse the button with the other fields.
74
The tabindex setting defines in which order the different fields should be activated when the
visitor clicks the tab key.
AN EXAMPLE:
75
JAVASCRIPT
JavaScript is a scripting language that allowsa programmer to add real programming to
webpages.
Small applications like a calculator or a primitive game of some sort can be created using
JavaScript.However, there are more serious uses for JavaScript:
Browser Detection
Detecting the browser used by a visitor at a page. Depending on the browser, another
page specifically designed for that browser can then be loaded.
Cookies
Storing information on the visitor's computer, then retrieving this information
automatically next time the user visits your page. This technique is called "cookies".
Control Browsers
Opening pages in customized windows, where you specify if the browser's buttons, menu
line, status line or whatever should be present.
Validate Forms
Validating inputs to fields before submitting a form.An example would be validating the
entered email address to see if it has an @ in it, since if not, it's not a valid address.
Since JavaScript isn't HTML, the browser should know in advance when JavaScript is entered to
an HTML page. This is done using the <script> tag.
The browser will use the <script> type="text/javascript"> and </script> to tell where JavaScript
starts and ends.
<html>
<head>
<title>My Javascript Page</title>
</head>
<body>
<script type="text/javascript">
alert("Welcome to my world!!!");
</script>
</body>
</html>
The word alert is a standard JavaScript command that causes an alert box to pop up on the
76
screen. The visitor will need to click the "OK" button in the alert box to proceed.
If we had not entered the <script> tags, the browser would simply recognize it as pure text, and
just write it on the screen.You can enter JavaScript in both the <head> and <body> sections of
the document.In general however, it is advisable to keep as much as possible in
the <head>section.
VARIABLES
Variables can be compared to small boxes with names.If you were to store 5 items, you might
have a box for each item. On each box you would write what is in it.
A variable is simply a place in the computer's memory to store information. All variables are
referred to by the unique name you assigned to them.
<html>
<head>
<title>My Javascript Page</title>
</head>
<body>
<script>
myname="Peter";
document.write(myname);
</script>
</body>
</html>
Note that when you want text to be stored in a variable you need to put the text in " ".
The reason is that JavaScript uses " " to tell the difference between text and variables.
77
<html>
<head>
<title>My Javascript Page</title>
</head>
<body>
<script>
Peter="my first name";
myname=Peter;
document.write(myname);
</script>
</body>
</html>
The result is that "my first name" will be written on the page.
if and else
Sometimes JavaScript requires the ability to make distinctions between different possibilities.
For example, you might have a script that checks which browser the user arrives with. If it's
MSIE, a page specifically designed for that browser should be loaded, if it's Netscape another
page should be loaded.
Also note that when comparing variables you will need to have two equals signs next to each
other (==).
If we wrote browser="MSIE" we would actually store "MSIE" in the variable called browser.
When you write browser=="MSIE" JavaScript knows that you want it to compare rather than
78
assign a value.
More complex if statements can be made by simply entering new if statements in the else part:
if (condition) {action1}
else {if (condition) {action2} else {action3};};
An example:
Ex: If the basket contains egg and the basket contains bacon, we can have egg and bacon.
Or is written as || and is used when more than a one condition should result in the check being
true. (|| is achieved by using the shift key combined with the \ key)
Ex: If the basket contains milk or the basket contains water, we can have something to
drink.
Not is written as ! and is used to invert the result.Ex: If not the basket contains egg or not the
basket contains bacon, we cant have egg and bacon.
Functions
79
Instead of just adding JavaScript to the page and having the browser perform the tasks as soon as
the script is read, you might want your JavaScript to be performed only upon the detection of a
certain event.
For example, if you made a JavaScript code that changed the background color of the page when
the user clicked a button, then you would need to tell the browser, that the script should not be
performed right away when loaded.
To keep the browser from performing a script as soon as it is loaded you need to write the script
as a function.
JavaScript written into functions will not be performed until you specifically ask for it. This way
you gain complete control of the timing.
<html>
<head>
<script>
function myfunction()
{
alert("Welcome to my world!!");
}
</script>
</head>
<body>
<form name="myform">
<input type="button" value="Hit me" onclick="myfunction()">
</form>
</body>
</html>
Click the button to see what the script in the example does:
If the line: alert("Welcome to my world!!"); had not been written within a function, it would
simply be performed as soon as the line was loaded.
But since we wrote it as a function, it was not performed until you hit the button.
As you can see, we placed the button in a form and added the event onClick="myfunction()" to
the properties of the button.
The next page gives a detailed description of the different events you could use to trigger
80
functions.
The { and the } marks the start and end of the function.
A typical bug when entering JavaScript functions is to forget about the importance of capitals in
javascript. The word function must be spelled exactly
function. Function or FUNCTION would cause an error.
Furthermore, use of capitals matters in the name of the function as well. If you had a function
called myfunction() it would cause an error if you referred to it
as Myfunction(), MYFUNCTION() or MyFunction().
LOOPS
Imagine that you wanted a script to perform the same routine over and over again 50 times in a
row.
Instead of adding 50 almost equal lines in your script you could use loops to make the script
perform a task like this.
The for loop is used when you know in advance how many times the script should perform.
For example if you wanted it to create exactly 50 lines.
The while loop is used when you want the loop to continue until a certain condition becomes
true.
For example, if you wanted to make a table comparing Celsius and Fahrenheit, stepping 15
degrees for each row, and you wanted the table to contain values up to 1200 degrees of Celsius.
FOR LOOPS:
81
SYNTAX:
for
(variable=startvalue; variable<=endvalue;variable=variable+incrementfacto
r)
{
// Here goes the script lines you want to loop.
}
EXAMPLE:
<html>
<head>
<title>Celsius-Fahrenheit Converter</title>
</head>
<body>
<table border=3>
<tr><td>CELSIUS</td><td>FAHRENHEIT</td></tr>
<script language="javascript">
for (celsius=0; celsius<=50; celsius=celsius+1)
{
document.write("<tr><td>"+celsius+"</td><td>"
+((celsius*9/5)+32)+"</td></tr>");
}
</script>
</table>
</body>
</html>
82
CELCIUS FAHRENHEIT
0 32
1 33.8
2 35.6
3 37.4
4 39.2
5 41
6 42.8
7 44.6
8 46.4
9 48.2
10 50
11 51.8
12 53.6
13 55.4
14 57.2
15 59
16 60.8
17 62.6
18 64.4
19 66.2
20 68
21 69.8
22 71.6
23 73.4
24 75.2
25 77
26 78.8
27 80.6
28 82.4
29 84.2
30 86
31 87.8
32 89.6
33 91.4
34 93.2
35 95
36 96.8
83
37 98.6
38 100.4
39 102.2
40 104
41 105.8
42 107.6
43 109.4
44 111.2
45 113
46 114.8
47 116.6
48 118.4
49 120.2
50 122
WHILE LOOPS:
SYNTAX:
while (variable<=endvalue)
{
// Here goes the script lines you want to loop.
}
Note: The <= could be anything that would fit the purpose ex. >, == or whatever.
EXAMPLE:
<html>
<head>
<title>Celsius-Fahrenheit converter</title>
</head>
<body>
<table border=3>
<tr><td>CELSIUS</td><td>FAHRENHEIT</td></tr>
<script language="javascript">
celsius=0;
while (celsius<=50)
{
document.write("<tr><td>"+celsius+
84
"</td><td>"+((celsius*9/5)+32)+"</td></tr>");
celsius=celsius+1;
}
</script>
</table>
</body>
</html>
break simply breaks the loop and continues with what might follow after the loop.
An example would be if you had a loop calculate the squareroot of numbers decrementing from
50.
Since calculating the square root of a negative number is an illegal mathemathic operation you
would like the loop to end when the square root of zero had been calculated.
if (value==0) {break};
continue breaks the current loop and continues with the next value.
An example would be if you had a loop that divided a certain value with a factor of numbers
ranging from -50 to +50.
Since division by zero is an illegal mathemathic procedure the loop would look like this:
ARRAYS
When working with more complex scripts you might face a situation in which you would have
many more or less similar variables. Instead of being forced to write a line for each operation
done to such a variable, you can use arrays to help you automate the process.
85
value3=30;
....
here would go 96 similar number=number+1)
lines { value[number]=number*10};
....
value100=1000
In example 1 you would need to enter 100 lines to perform an operation on your variables.
In example 2 you only need to enter 3 lines no matter how many variables you have.
Note: new must be written in small letters and Array must start with a capital A.
As the example indicated, arrays become extremely powerful when used in combination with
loops.
value[9]=170;
86