O B J E C T I V E S
In this chapter you will learn:
■ To understand important components of XHTML documents.
■ To use XHTML to create web pages.
■ To add images to web pages.
■ To create and use hyperlinks to navigate web pages.
■ To mark up lists of information.
■ To create tables with rows and columns of data and control table formatting.
■ To create and use forms to get user input.
■ To make web pages accessible to search engines using tags.
Introduction
Welcome to the world of opportunity created by the World Wide Web. The Internet is almost four decades old, but it wasn’t until the web’s growth in popularity in the 1990s and the recent start of the Web 2.0 era that the explosion of opportunity we are experienc- ing began. Exciting new developments occur almost daily—the pace of innovation is un- precedented. In this chapter, you’ll develop your own web pages. As the book proceeds, you’ll create increasingly appealing and powerful web pages. Later in the book, you’ll learn how to create complete web-based applications with databases and user interfaces.
This chapter begins unlocking the power of web-based application development with XHTML—the Extensible HyperText Markup Language. Later in the chapter, we intro- duce more sophisticated XHTML techniques such as internal linking for easier page nav- igation, forms for collecting information from a web-page visitor and tables, which are particularly useful for structuring information from databases (i.e., software that stores structured sets of data). In the next chapter, we discuss a technology called Cascading Style Sheets™ (CSS), a technology that makes web pages more visually appealing.
Unlike procedural programming languages such as C, C++, or Java, XHTML is a markup language that specifies the format of the text that is displayed in a web browser such as Microsoft’s Internet Explorer or Mozilla Firefox.
One key issue when using XHTML is the separation of the presentation of a docu- ment (i.e., the document’s appearance when rendered by a browser) from the structure of the document’s information. XHTML is based on HTML (HyperText Markup Lan- guage)—a legacy technology of the World Wide Web Consortium (W3C). In HTML, it was common to specify both the document’s structure and its formatting. Formatting might specify where the browser placed an element in a web page or the fonts and colors used to display an element. The XHTML 1.0 Strict recommendation (the version of XHTML that we use in this book) allows only a document’s structure to appear in a valid XHTML document, and not its formatting. Normally, such formatting is specified with Cascading Style Sheets (Chapter 5). All our examples in this chapter are based upon the XHTML 1.0 Strict Recommendation.
Editing XHTML
In this chapter, we write XHTML in its source-code form. We create XHTML documents by typing them in a text editor (e.g., Notepad, TextEdit, vi, emacs) and saving them with either an .html or an .htm filename extension.
Good Programming Practice 4.1
Assign filenames to documents that describe their functionality. This practice can help you iden- tify documents faster. It also helps people who want to link to a page, by giving them an easy-to- remember name. For example, if you are writing an XHTML document that contains product information, you might want to call it products.html. 4.1
Computers called web servers running specialized software store XHTML docu- ments. Clients (e.g., web browsers) request specific resources such as the XHTML docu- ments from web servers. For example, typing www.deitel.com/books/downloads.html into a web browser’s address field requests downloads.html from the books directory on the web server running at www.deitel.com. We discuss web servers in detail in Chapter 21. For now, we simply place the XHTML documents on our computer and render them by opening them locally with a web browser such as Internet Explorer or Firefox.
First XHTML Example
This chapter presents XHTML markup and provides screen captures that show how a browser renders (i.e., displays) the XHTML. You can download the examples from www.deitel.com/books/iw3htp4. Every XHTML document we show has line numbers for the reader’s convenience—these line numbers are not part of the XHTML documents. As you read this book, open each XHTML document in your web browser so you can view and interact with it as it was originally intended.
Figure 4.1 is an XHTML document named main.html. This first example displays the message “Welcome to XHTML!” in the browser. The key line in the program is line 13, which tells the browser to display “Welcome to XHTML!” Now let us consider each line of the program.
Lines 1–3 are required in XHTML documents to conform with proper XHTML syntax. For now, copy and paste these lines into each XHTML document you create. The meaning of these lines is discussed in detail in Chapter 14.
Lines 5–6 are XHTML comments. XHTML document creators insert comments to improve markup readability and describe the content of a document. Comments also help other people read and understand an XHTML document’s markup and content. Com- ments do not cause the browser to perform any action when the user loads the XHTML document into the web browser to view it. XHTML comments always start with . Each of our XHTML examples includes comments that specify the figure number and filename and provide a brief description of the example’s purpose. Subse- quent examples include comments in the markup, especially to highlight new features.
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 4.1: main.html -->
<!-- First XHTML example. -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Welcome</title>
</head>
<body>
<p>Welcome to XHTML!</p>
</body>
</html>
Fig. 4.1 | First XHTML example.
Good Programming Practice 4.2
Place comments throughout your markup. Comments help other programmers understand the markup, assist in debugging and list useful information that you do not want the browser to ren- der. Comments also help you understand your own markup when you revisit a document to mod- ify or update it in the future. 4.2
XHTML markup contains text that represents the content of a document and ele- ments that specify a document’s structure. Some important elements of an XHTML doc- ument are the html element, the head element and the body element. The html element encloses the head section (represented by the head element) and the body section (repre- sented by the body element). The head section contains information about the XHTML document, such as its title. The head section also can contain special document formatting instructions called style sheets and client-side programs called scripts for creating dynamic web pages. (We introduce style sheets in Chapter 5 and scripting with JavaScript in Chapter 6.) The body section contains the page’s content that the browser displays when the user visits the web page.
XHTML documents delimit an element with start and end tags. A start tag consists of the element name in angle brackets (e.g., ). An end tag consists of the element name preceded by a forward slash (/) in angle brackets (e.g., ). In this example, lines 7 and 15 define the start and end of the html element. Note that the end tag in line 15 has the same name as the start tag, but is preceded by a / inside the angle brackets. Many start tags have attributes that provide additional information about an element. Browsers can use this additional information to determine how to process the element.
Each attribute has a name and a value separated by an equals sign (=). Line 7 specifies a required attribute (xmlns) and value (http://www.w3.org/1999/xhtml) for the html ele- ment in an XHTML document. For now, simply copy and paste the html element start tag in line 7 into your XHTML documents. We discuss the details of the xmlns attribute in Chapter 14.
Common Programming Error 4.1
Not enclosing attribute values in either single or double quotes is a syntax error. However, some web browsers may still render the element correctly. 4.1
Common Programming Error 4.2
Using uppercase letters in an XHTML element or attribute name is a syntax error. However, some web browsers may still render the element correctly. 4.2
An XHTML document divides the html element into two sections—head and body. Lines 8–10 define the web page’s head section with a head element. Line 9 specifies a title element. This is called a nested element because it is enclosed in the head element’s start and end tags. The head element is also a nested element because it is enclosed in the html
element’s start and end tags. The title element describes the web page. Titles usually appear in the title bar at the top of the browser window, in the browser tab that the page is displayed on, and also as the text identifying a page when users add the page to their list of Favorites or Bookmarks that enables them to return to their favorite sites. Search engines (i.e., sites that allow users to search the web) also use the title for indexing purposes.
Good Programming Practice 4.3
Indenting nested elements emphasizes a document’s structure and promotes readability.
Common Programming Error 4.3
XHTML does not permit tags to overlap—a nested element’s end tag must appear in the docu- ment before the enclosing element’s end tag. For example, the nested XHTML tags
hello cause a syntax error, because the enclosing head element’s ending tag appears before the nested title element’s ending tag.
Good Programming Practice 4.4
Use a consistent title-naming convention for all pages on a site. For example, if a site is named “Bailey’s Website,” then the title of the contact page might be “Bailey’s Website - Contact.” This practice can help users better understand the website’s structure. 4.4
Line 12 begins the document’s body element. The body section of an XHTML doc- ument specifies the document’s content, which may include text and elements.
Some elements, such as the paragraph element (denoted with
and
) in line 13, mark up text for display in a browser. All the text placed between theand
tags forms one paragraph. When the browser renders a paragraph, a blank line usually pre- cedes and follows paragraph text.
This document ends with two end tags (lines 14–15). These tags close the body and html elements, respectively. The