By Anand Vishwanathan
Web Designing
Contents
CONTENTS THE INTERNET ......................................................................................................................... 3 What is the Internet? ............................................................................................................... 3 What are the various Applications of the Internet? ................................................................... 3 Web Browsing / Surfing........................................................................................................... 3 Servers, Protocols and Pathnames ......................................................................................... 3 WEB - The Mechanism ........................................................................................................... 3 HTML......................................................................................................................................... 5 HTML - A Markup Language ................................................................................................... 5 What HTML does? .................................................................................................................. 5 What HTML does not do? ....................................................................................................... 5 What do you need to start designing Web Pages?................................................................... 5 Your First Page....................................................................................................................... 6 The HEAD of a Page............................................................................................................... 6 The BODY of a Page .............................................................................................................. 7 Text on your Page ............................................................................................................... 8 My First Page ...................................................................................................................... 9 Hyperlinks ........................................................................................................................... 9 Tables ................................................................................................................................10 Images ...............................................................................................................................11 Image Maps........................................................................................................................12 Lists....................................................................................................................................13 Forms.................................................................................................................................13 Frames...............................................................................................................................14 Layers ................................................................................................................................16 Layers and IE .....................................................................................................................18 DHTML .....................................................................................................................................19 What is DHTML?....................................................................................................................19 Cascading Style Sheets (CSS)................................................................................................20 JavaScript ................................................................................................................................26 What is Javascript?................................................................................................................26 What are the alternatives?......................................................................................................26 How to write JavaScript?........................................................................................................27 Comments..........................................................................................................................27 Variables ............................................................................................................................28 Operators ...........................................................................................................................28 Input and Output.................................................................................................................28 1
Web Designing
Contents
Conditional Statement ........................................................................................................29 Loops .................................................................................................................................29 Arrays.................................................................................................................................30 Functions............................................................................................................................31 JavaScript and Mathematics ..................................................................................................31 Cookies..................................................................................................................................32 What is a cookie?...................................................................................................................32 How to write a Cookie?.......................................................................................................33 How to read a Cookie? .......................................................................................................33 How to delete a cookie?......................................................................................................35 Examples of JavaScript..........................................................................................................35 Document Object Model (DOM) ..............................................................................................37 What is the DOM?..................................................................................................................37 The BODY object ...................................................................................................................38 Name your Objects.............................................................................................................38 How to access your Objects................................................................................................38 Modify Objects....................................................................................................................38 Some functions in the document object...............................................................................39 Events and Event Handlers.................................................................................................39 The Window Object................................................................................................................40 Form Processing through JavaScript......................................................................................41 Form Validation ..................................................................................................................42 JavaScript and Frames ..........................................................................................................43 Web Resources .......................................................................................................................45
2
Web Designing
The Internet
THE INTERNET What is the Internet? The Internet is a network of many smaller networks that exist all over the world. It contains millions and millions of pages of information waiting for you to access. All the computers connected to the Internet share equal rights i.e. there is no central controller.
What are the various Applications of the Internet? The main applications of the Internet are as follows: SERVICE HTTP SMTP and POP FTP Telnet
PROTOCOL USED Web Browsing E-mail Transferring files across the Internet To log on and work on another system as though you are sitting just next to it.
Web Browsing / Surfing One of the main applications of the Internet is Browsing or Surfing, as it is more popularly known as. This helps one to get information from across the globe. In order to access a Web Site, you need a software called a Web Browser and the address of the Site. These addresses are also referred to as URLs. Some examples include http://www.hotmail.com, http://www.harward.edu, etc. If you look closely at these names you will find certain similarities.
Servers, Protocols and Pathnames In order that so many computers can be networked and communicate with each other, they should use a common protocol or a language. A protocol is a standard way to send and receive information. Each file on the Internet has a unique address called a Uniform Resource Locator (URL). It is determined by the type of protocol, name of the server on the Internet and location of the file on the Internet. Let us analyze the URL http://www.hotmail.com. The http stands for Hypertext Transfer Protocol. The www indicates World Wide Web whereas the .com indicates a commercial Site. Other suffixes include ".gov", a Government Site, ".edu", an Educational Institution's Site, ".org", an organization's Site, etc.
WEB - The Mechanism Let us suppose you type in an address, say http://www.hotmail.com, in your Browser. Then the following steps are executed before the page actually shows up on your screen: • By typing the URL in your Browser, you have requested for the Home Page of Hotmail. Note that the default web page (i.e. the on which will be loaded the first) on most Web Servers is index.htm (or index.html) or content.htm (or content.html).
3
Web Designing
•
•
•
The Internet
Hence, typing http://www.hotmail.com is equivalent to typing http://www.hotmail.com/index.htm. Next, your Browser needs the IP address of Hotmail's Server. The IP Address consists of four numbers (0 - 255) each separated by a (.) (e.g. 202.54.1.180. Every computer on the Internet is assigned a unique IP Address. Now, of course, it is not possible for a human being to remember all these numbers. Hence, a mechanism known as DNS (Domain Naming System) is used. Thus Web Sites can be referred to by their Domain Names only. This means that at some stage the name has to be translated to the IP address. This is done by DNS Servers. Your Browser will contact your nearest DNS Server to get the IP address of Hotmail (say, VSNL). If VSNL's server knows the IP address, it will send it to your Browser. Else, it will contact another DNS Server. This continues until the top level DNS Servers are contacted. If the IP is still not found, your Browser returns a "Web Site not found" message. If IP is found, your Browser automatically connects to the Hotmail Server and requests the page(s) required.
Nowadays, Browsers have a History (like IE) or maintain a cache (like Netscape) of recently visited Sites. If you visit Hotmail regularly, it will reload quickly and directly from the Cache itself. If your page is partly downloaded and seems to have frozen, it is probably because a packet has been lost on its way from Hotmail to your Desktop. Try refreshing your Browser.
4
Web Designing
HTML
HTML HTML - A Markup Language You are probably under the impression that HTML is a difficult-to-learn language like C/C++. This is not true. HTML stands for HyperText Markup Language. It is associated with the formatting and positioning of text, images, etc. You can compare it with the ".doc" files created by MS Word. Then why is HTML so popular? The reason is that unlike the various formats which are application and platform dependent, HTML can be universally used. To put it in other words, an HTML file can be viewed on a Windows machine or Linux or even a Unix machine. Since, on the Internet any machine can be used, we need a generalized format for presenting information. Here comes the use of HTML.
What HTML does? We have seen that HTML is not a programming language. Then why is it used on the Internet? Let us suppose that you want to create a page about yourself to be posted on the Internet. This page will include your name, age, address, hobbies, etc. and even your photograph. You want the page heading to be "About Me", followed by your photograph at the center, etc. HTML allows you to position, align and format Text as well as images. You can even include tables, forms for h4andling user input, etc.
What HTML does not do? Although HTML is quite capable of presenting information neatly and in an organized manner, it is capable of creating only, what are called, "Static Pages". You are not able to add interactivity to your Site. As a result, your viewers will get bored and will never come back to your Site. To overcome this drawback, Web Designers today use DHTML, JavaScript, Flash, etc. Thus, a truly professional Web Site should include all the technologies available today to stand out amongst the others.
What do you need to start designing Web Pages? •
• • •
HTML Editor: To type all the code in. Even though a normal text editor like Notepad will do, most Web Designers prefer using professional packages such as Frontpage, Dreamweaver, etc. Most of these provide a WYSIWYG (What You See Is What You Get) layout. Web Browser: To view all that you create. Webspace: a place to put up your creation and show off to the others. Some amount of creativity and patience.
Once you have conceptualised and completed your Site, you need to put it up on the Internet for people to admire. You can either hire space or use the space provided by certain free Web Space Providers. My personal favourite is Tripod (http://www.tripod.com) which provides you upto 15MB space along with freebies like Guestbooks, Counters, etc.)
5
Web Designing
HTML
Your First Page We'll now create the simple "Hello World!" page. Type the following onto your HTML Editor:
My first Page Hello World!
Save the file with a ".htm" (or ".html") extension and view it in your Browser. Congrats! You have created your first HTML page. Let us analyze the code line by line: 1. Every HTML file should begin with and end with the … tags b) BODY: identified by the … tags. 3. Every page can also have a title enclosed in the
… tag. The title is displayed in the Title Bar of the Browser Window and is used while saving Bookmarks. Only one title can appear in a document, it cannot contain links or highlighting. Now we shall analyze the HEAD and BODY sections in greater detail and, at the same time, study the various tags available in HTML.
The HEAD of a Page This section is not displayed directly to the user. It is used to include Page Titles, Keywords, etc. which are used by the Browser for various purposes. The typical head section of a page will look like
My first Page <meta name="keywords" content="About Me"> <meta name="description" content="My first Web Page created in a Text Editor."> The Meta Tags serve various purposes. One of the most important of these is the indexing of the Site in a Search Engine. Most Search Engines divide Web Sites into various categories. They look for specific Meta Tags in the HEAD section and, accordingly, index your Site in the appropriate category. The "keywords" and the "description" are used for this purpose. There is yet another application of Meta Tags. This allows you to add some interactivity to your page.
My first Page <meta http-equiv="refresh" content="5"> 6
Web Designing
HTML
This piece of code automatically refreshes your page after 5 seconds. Similarly, you can redirect your user to any other URL by the following code:
My first Page <meta http-equiv="refresh" content="0;URL=newpage.htm"> This automatically redirects the Browser to newpage.htm.
Now that you have come across the Meta Refresh Tag, can you think of any interesting application? Let me give you a clue, • The first piece of code was used by a certain Site to display Cricket Scores Live during the last World Cup. • You can create a slideshow using the second example. Think about it. The
tag sets the default font size, colour and typeface of a particular document. With this, the HEAD section of your Page is completed.
The BODY of a Page This section contains all the text, images, hyperlinks and other material to be displayed on the page, together with all the HTML formatting codes that control the layout of the page. The tag has numerous attributes that control the layout of the page. The are listed below: Attribute Text Bgcolor Background Link Vlink Alink Topmargin Leftmargin
Description Decides the default colour of all the text on the page Defines the default Background Colour of the page Defines the Background Image. This image is tiled throughout the page Defines the default colour of all the hyperlinks in the page Defines the default colour of all the visited hyperlinks in the page Defines the default colour of the active hyperlink in the page Defines, in pixels, the topmargin space Defines, in pixels, the leftmargin space
Usage Text = "green" Bgcolor="#FFFFFF" background="image1.gif" link="#0000FF" vlink="#FF00FF" Alink="#FF0000" topmargin="0" Leftmargin="0"
As you might have already noticed, I have referred to the text as green whereas the bgcolor is #FFFFFF. Well, HTML supports two colour schemes. Colours are defined on a RGB scale. Here Red has a value #FF0000, Green has that of #00FF00 and Blue has a value of #0000FF. Can you guess the value of White and Black? To make life easier for the Designer, certain standard colours can be referred to by their names. These include aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, yellow, white 7
Web Designing
HTML
and some others. But, not all browsers support this naming scheme. So Beware! Secondly, Internet widely supports the GIF and JPEG image formats. The former is preferred for Web Buttons while the latter is used for Photos.
Text on your Page Okay, now that you have designed the look of your page, how about actually entering some text in it. Text on a Web Page is entered in a paragraph and is enclosed within the
. . .
tags. This tag takes the align (=left, center, right) attribute. A line break can be inserted with the
tag. You can change the font of the text using the
. . . tags. This tag takes in the following attributes: Color = color; Set the colour of enclosed Text Face = list; Set the typeface of the enclosed text Size = value; set the size to an absolute value (1 to 7)
Web Fonts: When your Browser comes across a font face tag, it checks to see if the font is loaded on your system. If not, it tries to use the most similar font. Hence, the effect won't be the same as the one the Web Designer wanted. Hence, refrain from using too fancy-looking fonts on your page. The fonts you can safely use are Arial, Courier, Helvetica, Times New Roman and Verdana. Later I will tell you how to use those special Text Effects on your Web Site without having to worry about the font face. Lets study some more tags which can appear within the
tag. Tag . . . . . . . . .
Description Underlines the Text within the tag Makes the text appear Bold Converts the text to italics
Apart from the paragraph tag, HTML also provides Heading tags. You can use upto 6 headers in a page. These tags are
. . .
,
. . .
and so on till
. . .
. Now that we have learnt how to present text, we will write some code.
My first page My first page
About Me
Hi, how are you?
I am fine thank you.
8
Web Designing
HTML
This page will appear as
My First Page About Me Hi, How are you? I am fine thank you.
Note that HTML tags are defined between '<' and '>' signs. So what do you do when you want to write a sentence like "x < y"? For this reason, HTML defines certain character entities for the above two character and also some other characters like: Symbol [space] & >
Named Entity & >
Symbol " < ©
Named Entity " < ©
Thus, the above statement could be written as "x < y" Some other HTML tags are:
. . . . . . . . . . . . . . .
. . .
. . . Please refer the attached list of tags for further details.
Hyperlinks We discussed about URLs earlier. Every page on the Web must have a unique URL or address. Hence, any Web Page can be linked to any other Web Page or a resource through its URL. The HTML tag used for this is
. . . , also called the anchor tag. For example, to guide a user from "My first Page" to "My Interests" (say, interests.htm), we could have a text on the page which reads "Click here to know my interests". The user, on clicking the link will be directed to interests.htm. The code for this will appear as follows:
Click here to know my interests The text within the tags will appear in the colour defined in the link attribute in the body tag. You can create a hyperlink to not only another page, but also to a specific portion of your page. This can be done by defining anchors in your page as shown in the example below.
Click here to see my address> . . .
Address: Mumbai Try out this tag. The Web works only because of linking of Web Pages together.
9
Web Designing
HTML
When you use frames, you need to specify the target attribute of the hyperlink. This attribute will contain the Frame Name where you want the Hyperlink to take effect. The following are various targets supported by the Browsers: Target _blank _parent _self _top
Description Loads Document in new Window Loads Document in parent of Current Frame Loads Document in same Frame Loads document in same Window
Directory Structure on the Internet Now that you know how to link Web Pages, you can start trying your hand at Designing small Sites. Your site will have, say, 5 pages. You can store your pages in different folders. How do you link to pages in a different sub-directory. Directory names are separated by a front slash "/". You can thus use relative paths on the Web. To go on level higher, use "../"
Tables So, what do you know now? Well, you know how to display text on a Web Page. You also know how to align your text, change it font face, colour and create hyperlinks. But in the world of Web Design, this will not take you far. Every Web Designer has to master the skills of creating tables consisting of rows and columns. Let us see how this is done? A table comprises three basic elements • the table itself, • the table rows and • the table columns. And so, when playing with tables, there are three tags you need to burn into your memory: The
tag is the main tag used to create a table. The
|
tag is used to define a horizontal row. Each row may be further divided into 'cells'. The
| tag is used to define cells (or columns) in a row. Pretty simple, right? The
tags always appear within | tags, which in turn are always sandwiched between the tags. Now we'll create a simple table with two rows and three columns: Row 1, Column 1 | R1,C2 | R1,C3 |
Row 2,Column 1 | R2,C2 | R3,C3 |
10
Web Designing
HTML
You should see something like this: Row 1, Column 1 Row 2, Column 1
R1, C2 R2, C2
R1, C3 R3, C3
Note: Please refer to the various attributes of the tag provided. Next, we'll see how to stretch rows and columns. To stretch (i.e. merge) two columns, use the COLSPAN attribute within the tag. Thus, Row 1, Column 1 | R1,C2 and C3 |
Row 2,Column 1 | R2,C2 | R3,C3 |
will result in Row 1, Column 1 Row 2, Column 1
R1, C2 and C3 R2, C2 R3, C3
Similarly, Rows can be merged using the ROWSPAN attribute.
Images Statutory Warning: Adding images enhances the look of a page, but they also reduce the speed at which a page downloads on the Net. The above statement is very true. In fact, the Web Designers worst nightmare is to make his page look attractive without causing his viewers to wait for ages for the page to download. This, however, does not mean that you should avoid images altogether. Instead, you should design your page optimizing both speed and appearance. As explained before, the Internet supports GIF, JPEG, ART formats. Of these, GIF is used for buttons and JPEG for photos. The
tag is used for inserting images in Web Pages. It has the following attributes. Attribute Src=url Align Height Width Border Alt
Description Source URL of the image to be displayed Align the image to the top, middle, bottom, left or right of text in the line. Height of image in pixels Width of image in pixels Thickness of border around the image in pixels Specifies alternative text to be displayed in place of the image, if browser does not support images or image handling is turned off. 11
Web Designing
HTML
Consider the following example:
This will display the image "photo.jpg" on your Browser. How about making an image as a hyperlink. Viewers can then click on an image to navigate your Site. The code will be similar to
Note that if you do not specify the image width and height, the Browser will render the image in its default size.
Image Maps An image map is a single map that sends you to different places depending on which part of it you click. They come in two flavours, • •
Server-side maps, which are dependent on and interpreted by the remote server. Client-side maps, which are defined in regular HTML documents and are processed by the Web Browser.
An image map specification consists of one or more elements enclosed within a sandwich. The