www.gibmonks.com

Main Page

Previous Page
Next Page

[Page 1331 (continued)]

J.5. Linking

One of the most important XHTML features is the hyperlink, which references (or links to) other resources, such as XHTML documents and images. In XHTML, both text and images can act as hyperlinks. Web browsers typically underline text hyperlinks and color their text blue by default, so that users can distinguish hyperlinks from plain text. In Fig. J.3, we create text hyperlinks to four different Web sites. Line 17 introduces the <strong> tag. Browsers typically display text marked up with <strong> in a bold font.

Figure J.3. Linking to other Web pages.
(This item is displayed on pages 1331 - 1332 in the print version)

 1  <?xml version = "1.0"?>
 2  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 3     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 4
 5  <!-- Fig. J.3: links.html        -->
 6  <!-- Introduction to hyperlinks. -->
 7
 8  <html xmlns = "http://www.w3.org/1999/xhtml">
 9     <head>
10        <title>Introduction to hyperlinks</title>
11     </head>
12
13     <body>
14
15        <h1>Here are my favorite sites</h1>
16
17        <p><strong>Click a name to go to that page.</strong></p>
18
19        <!-- create four text hyperlinks -->
20        <p>
21           <a href = "http://www.deitel.com">Deitel</a>
22        </p>
23
24        <p>
25           <a href = "http://www.prenhall.com">Prentice Hall</a>
26        </p>
27
28        <p>
29           <a href = "http://www.yahoo.com">Yahoo!</a>
30        </p>
31
32        <p>
33           <a href = "http://www.usatoday.com">USA Today</a>
34        </p>
35
36     </body>
37  </html>

Links are created using the a (anchor) element. Line 21 defines a hyperlink that links the text Deitel to the URL assigned to attribute href, which specifies the location of a linked resource, such as a Web page, a file or an e-mail address. This particular anchor element links to a Web page located at http://www.deitel.com. When a URL does not indicate a specific document on the Web site, the Web server returns a default Web page. This page often is called index.html; however, most Web servers can be configured to use any file as the default Web page for the site. (Open http://www.deitel.com in one browser window and http://www.deitel.com/index.html in a second browser window to confirm that they are identical.) If the Web server cannot locate a requested document, the server returns an error indication to the Web browser, and the browser displays an error message to the user.


[Page 1333]

Anchors can link to e-mail addresses through a mailto: URL. When someone clicks this type of anchored link, most browsers launch the default e-mail program (e.g., Outlook Express) to enable the user to write an e-mail message to the linked address. Figure J.4 demonstrates this type of anchor.

Figure J.4. Linking to an e-mail address.
(This item is displayed on pages 1333 - 1334 in the print version)

 1  <?xml version = "1.0"?>
 2  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 3     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 4
 5  <!-- Fig. J.4: contact.html   -->
 6  <!-- Adding email hyperlinks. -->
 7
 8  <html xmlns = "http://www.w3.org/1999/xhtml">
 9     <head>
10        <title>Adding e-mail hyperlinks</title>
11     </head>
12
13     <body>
14
15        <p>My email address is
16           <a href = "mailto:deitel@deitel.com">
17              deitel@deitel.com
18           </a>
19           . Click the address and your browser will
20           open an e-mail message and address it to me.
21        </p>
22     </body>
23  </html>

Lines 1719 contain an e-mail link. The form of an e-mail anchor is <a href = "mailto:emailaddress">...</a>. In this case, we link to the e-mail address deitel@deitel.com.


Previous Page
Next Page