Links

Without hyperlinks, the Web wouldn’t be “the Web”. This section explains how to create links to other websites (external links) and links within your own website (internal links).

The a Element

The anchor element, a, creates links. Here’s an example:

Example 2.12. External Links

<p>
A link to <a href="http://www.google.com/">Google's
home page</a>.
</p>
<p>
A link to
<a href="http://www.google.com/help/basics.html">a
page within Google</a>.
</p>

The syntax is straightforward: the href attribute indicates where the link should go, and the text between the <a> and </a> tags becomes the highlighted link text.

Anchor Titles

The optional title attribute allows you to provide a little extra information about the link. Some older browsers ignore this attribute, but most modern browsers produce a little floating “tooltip” if you hover your mouse over the link.

Example 2.13. The title Attribute

Can physics be
<a href="http://www.dctech.com/physics/humor.php"
title="The most hilarious collection of physics
humor anywhere!">funny</a>?
Is that a rhetorical question?

You can use the title attribute to indicate where the link is going or provide some other useful information. It’s nifty, but it doesn’t hurt to leave it out, either.

Components of the URL

To construct links to other websites, we need to understand the structure of the URL. Let’s take a closer look at the URL “http://www.google.com/help/basics.html”:

Wait, Which File Was That Again?

This is a little odd — in our first example, “http://www.google.com/”, we didn’t specify a file or a directory. So what did we ask for, exactly?

First, it turns out the directory is specified. The single forward slash (“/”) represents the “root” or “top-level” directory of the website. We’re asking Google to give us a file from the top directory of its website.

Okay, but which file are we asking for? We didn’t specify the name of the file explicitly, we just asked for a directory. This means means we’re asking Google, “Give us the default file for that directory, if you have one.” As it turns out, Google is going to give us a file named “index.html”, but the exact name could be anything. It’s up to Google.

Internal links

Internal links are similar to external links except that you can leave off the protocol and website name. You only need to specify the file path.

File paths for internal links can be absolute or relative. Absolute links always begin with a forward slash. They specify a path that starts at the root directory of your web server. Relative links never begin with a forward slash. They specify a path that is relative to the page that the link is on.

Examples of absolute links:

Examples of relative links (relative to this page, http://www.goer.org/HTML/basic/links/index.html):

As the examples demonstrate, URLs can vary in complexity while still pointing to the same location.

For pages that are “near” the current page, relative links are often simpler. Relative links also work better if you are working with files on your desktop, as opposed to a real websever.

Because absolute links are the same no matter where you are on the website, they’re good for standard navigation links, header and footer links. However, absolute links don’t work as well if you are just messing around with files on your desktop. On a webserver, an absolute link such as /HTML/index.html starts with the webserver’s root directory. But on your local desktop, there is no “webserver root directory”, and so your computer attempts to follow the filepath /HTML/index.html from the root directory of your hard drive. In general, this will fail.

Linking Within Pages

We can link to web pages in external websites and internal websites. What about linking to a particular location in a page?

All elements provide this capability through the id attribute. Once you’ve assigned an id to the element, you can link directly to the location on the page where the element appears:

Example 2.14. Linking to an ID

<h2 id="fruitbat_migratory">
Migratory Patterns of the Fruit Bat
</h2>
...
...
...
<p>
<a href="#fruitbat_migratory">link to 'Migratory Patterns of the Fruit Bat'</a>.
</p>
...

The id’s value is fruitbat_migratory. Further down the page, we have a link to this ID, specified by href="#fruitbat_migratory".

You can combine named anchors with longer URLs: for example, http://www.goer.org/HTML/basic/links/#parts.

The pound sign in the URL makes the link point to the named anchor. If we had accidentally written href="parts", that would have been a relative link to the directory named “parts”.

Caution

Never give two elements the same ID on the same page.