Chapter 5
In This Chapter
Understanding hyperlinks
Building the anchor tag
Recognizing absolute and relative links
Building internal links
Creating lists of links
The basic concept of the hyperlink is common today, but it was a major breakthrough back in the day. The idea is still pretty phenomenal, if you think about it: When you click a certain piece of text (or a designated image, for that matter), your browser is instantly transported somewhere else. The new destination might be on the same computer as the initial page, or it could be literally anywhere in the world.
Any page is theoretically a threshold to any other page, and all information has the ability to be linked. This is still a profound idea. In this chapter, you discover how to add links to your pages.
The hyperlink is truly a wonderful thing. Believe it or not, there was a time when you had to manually type in the address of the web page you wanted to go to. Not so anymore. Figure 5-1 illustrates a page that describes some of my favorite websites.
In Figure 5-1, the underlined words are hyperlinks. Clicking a hyperlink takes you to the indicated website. Although this is undoubtedly familiar to you as a web user, a few details are necessary to make this mechanism work:
The key to hypertext is an oddly named tag called the anchor tag. This tag is encased in an <a></a> set of tags and contains all the information needed to manage links between pages.
The code for the basicLinks.html page is shown here:
<!DOCTYPE html>
<html lang = "en-US">
<head>
<meta charset = "UTF-8">
<title>basicLinks.html</title>
</head>
<body>
<h1>Some of my favorite sites</h1>
<h2>Wikipedia</h2>
<p>
One of my favorite websites is called
<a href = "http://www.wikipedia.org">wikipedia.</a>
This terrific site allows ordinary users to enter
encyclopedia definitions. Over time, the entries
can be as reliable as a commercial encyclopedia,
and a lot more complete.
</p>
<h2>Dummies</h2>
<p>
You can find out a lot about upcoming and current
Dummies books at <a href = "http://www.dummies.com">
www.dummies.com</a>. You might even find this
book mentioned there.
</p>
<h2>PopURLS</h2>
<p>
If you want
to know what's happening on the Internet today,
check out <a href = "http://popurls.com">
popurls.com</a>. This site aggregates a bunch of
social networking sites.
</p>
</body>
</html>
As you can see, the anchor tag is embedded into paragraphs. The text generally flows around an anchor, and you can see the anchor code is embedded inside the paragraphs.
All the tags described so far in this book have been block-level tags. Block-level tags typically begin and end with carriage returns. For example, three <h1> tags occupy three lines. Each <p></p> set has implied space above and below it. Most HTML tags are block-level.
Some tags are meant to be embedded inside block-level tags and don't interrupt the flow of the text. The anchor tag is one such tag. Anchors never stand on their own in the HTML body. This type of tag is an inline tag. They're meant to be embedded inside block-level tags, such as list items, paragraphs, and headings.
The first link shows all the main parts of an anchor in a pretty straightforward way:
<a href = "http://www.wikipedia.org">wikipedia.</a>
The special link addresses are a very important part of the web. You probably already type web addresses into the address bar of your browser (http://www.google.com), but you may not be completely aware of how they work. Web addresses are technically URLs (Uniform Resource Locators), and they have a very specific format.
A URL usually contains the following parts:
Domain |
Explanation |
.org |
Non-profit institution |
.com |
Commercial enterprise |
.edu |
Educational institution |
.gov |
Governing body |
.ca |
Canada |
.uk |
United Kingdom |
.tv |
Tuvali |
Many web pages turn out to be lists of links. Because lists and links go so well together, it's good to look at an example. Figure 5-2 illustrates a list of links to books written by a certain (cough) devilishly handsome author.
This example has no new code to figure out, but the page shows some interesting components:
This code shows the way the page is organized:
<!DOCTYPE html>
<html lang = "en-US">
<head>
<meta charset = "UTF-8">
<title>listLinks.html</title>
</head>
<body>
<h1>Some nice programming books</h1>
<ul>
<li><a href = "http://www.aharrisbooks.net/haio">
HTML / CSS / JavaScript ALL in One for Dummies</a>
A complete resource to web development</li>
<li><a href = "http://www.aharrisbooks.net/jad">
JavaScript / AJAX for Dummies</a>
Using JavaScript, AJAX, and jQuery</li>
<li><a href="http://www.aharrisbooks.net/pythonGame">
Game Programming - the L Line</a>
Game development in Python</li>
<li><a href="http://www.aharrisbooks.net/h5g">
HTML5 Game Development for Dummies</a>
Building web and mobile games in HTML5</li>
</ul>
</body>
</html>
The indentation is interesting here. Each list item contains an anchor and some descriptive text. To keep the code organized, web developers tend to place the anchor inside the list item. The address sometimes goes on a new line if it's long, with the anchor text on a new line and the description on succeeding lines. I normally put the <li> tag at the end of the last line, so the beginning <li> tags look like the bullets of an unordered list. This makes it easier to find your place when editing a list later.
There's more than one kind of address. So far, you've seen only absolute references, used for links to outside pages. Another kind of reference — a relative reference — links multiple pages inside your own website.
The type of link used in basicLinks.html is an absolute reference. Absolute references always begin with the protocol name (usually http://). An absolute reference is the complete address to a web page, just as you'd use in the browser's address bar. Absolute references are used to refer to a site somewhere else on the Internet. Even if your website moves (say, from your desktop machine to a web server somewhere on the Internet), all the absolute references will work fine because they don't rely on the current page's position for any information.
Relative references are used when your website includes more than one page. You might choose to have several pages and a link mechanism for moving among them. Figure 5-3 shows a page with several links on it.
The page isn't so interesting on its own, but it isn't meant to stand alone. When you click one of the links, you go to a brand-new page. Figure 5-4 shows what happens when you click the market link.
The market page is pretty simple, but it also contains a link back to the initial page. Most websites aren't single pages at all, but an interconnected web of pages. The relative reference is very useful when you have a set of pages with interlacing links.
The code for pigs.html shows how relative references work:
<!DOCTYPE html>
<html lang = "en-US">
<head>
<meta charset = "UTF-8">
<title>pigs.html</title>
</head>
<body>
<h1>Destinations of Porcine Mammals</h1>
<ul>
<li>This little pig went to
<a href = "market.html">market</a></li>
<li>This little pig stayed
<a href = "home.html">home</a>.</li>
<li>This little pig had
<a href = "roastBeef.html">roast beef</a></li>
<li>This little pig had
<a href = "none.html">none</a>.</li>
<li>This little pig went
<a href = "wee.html">'wee wee wee'</a>
all the way home.</li>
</ul>
</body>
</html>
Most of the code is completely familiar. The only thing surprising is what's not there. Take a closer look at one of the links:
<a href = "market.html">home</a>.</li>
There's no protocol (the http:// part) and no address at all, just a filename. This is a relative reference. Relative references work by assuming the address of the current page. When the user clicks market.html, the browser sees no protocol, so it assumes that market.html is in the same directory on the same server as pigs.html.
Relative references work like directions. For example, if you're in my lab and ask where the water fountain is, I'd say, “Go out into the hallway, turn left, and turn left again at the end of the next hallway.” Those directions get you to the water fountain if you start in the right place. If you're somewhere else and you follow the same directions, you don't really know where you'll end up.
Relative references work well when you have a bunch of interconnected web pages. If you create a lot of pages about the same topic and put them in the same directory, you can use relative references between the pages. If you decide to move your pages to another server, all the links still work correctly.
If you're referring to a page on somebody else's site, you have to use an absolute reference. If you're linking to another page on your site, you typically use a relative reference.