The <A>
tag actually serves two purposes: Creating a link to another resource on
the Web, and creating a fragment identifier within a document.
The basics:
Creating a link
When used to create a link, the <A>
tag never appears without the HREF
attribute. This attribute supplies the URL of the resource to which the
link refers. The content of the tag (i.e., everything between the <A>
and </A> tags)
is displayed differently, telling the user that it is a link. Usually,
it is presented in a different color, with an underline, or (in the case
of images) with a colored border.
A typical <A>
tag might look like this:
<A HREF="http://finalhost.com"><B>Final
Host</B></A>
The target of this link is the URL of finalhost.
Within the document, the words "Final Host" will be displayed
as a link, like this: Final Host.
To be honest, there is little more to say about using
the <A> tag to
create links. However, there is much more to say about how you create
those links.
Relative and
absolute links
Technically, every URL has two parts: The scheme
and the scheme-specific part. URLs that point to other documents
on the Web use the http
scheme; other common URLs use the ftp,
mailto, and nntp
schemes. Each has a unique scheme-specific part that provides all the
details the browser needs to access that resource.
The http
URL requires you to provide several bits of information, including a
server name, a document directory, and a document name. Most, if not
all, of these components are optional, and there are rules that let the
browser fill in the missing pieces if you don't provide them.
If you do provide all the pieces, the URL is said to be
absolute: It absolutely defines where the resource can be found on the
Web. If one or more pieces are missing, the URL is said to be relative:
The missing pieces are copied from the document containing the <A>
tag. This makes the value of the URL dependent upon, and relative to,
the location of the containing document. Here are some samples:
http://finalhost.com/soundadvice/
buildingblocks.html
//finalhost.com
http:buildingblocks.html
/soundadvice
Only the first URL is absolute: It completely specifies
the scheme, the server, the directory, and the document to be retrieved.
In fact, it is the URL of this document. The second URL is relative,
because it is missing the scheme. The browser will use the scheme of the
owning document. The third URL is relative, since it is missing a server
name and a document directory. Again, the browser will use the server
name and document directory of the containing document to fill in the
missing pieces. The last URL is missing the scheme and server, but
supplies a document directory. Again, the browser picks up the
containing document's scheme and server name.
Which should
you use?
Both kinds of URLs are useful. In general, you should
use absolute URLs when referencing resources that are on a server
different from the current document's, or whose server is not likely to
change.
Relative URLs are much more useful. You should use
relative URLs when referencing documents on the same server, in the same
collection, or within the same directory structure. A good rule is to
eliminate as many elements of the URL as you can and still have it work
correctly.
Why is this? An important feature of Web-based documents
that is often overlooked is portability. If you use relative
links throughout your documents, you can move these documents to a
different directory, a different server, or even a different scheme
without ever changing the internal links.
Consider a collection of related documents that link to
one another. If you place all of these documents in the same directory
on your server, you can create links using only the document names,
removing the scheme, server name, and document directory. If you choose
to move the collection to a different directory, the links will work
correctly, inheriting the directory from the first document in the
collection you access. If you get more ambitious and move the whole
directory to a different server, the links will still work, inheriting
the server name in a similar manner. If you decide to deliver a static
version of this directory via CD (changing the scheme to
"file"), the links will still work.
Relative URLs take a little extra work, creating the
right document collection architecture, but pay off in reduced
maintenance costs and ease of portability.
<< BACK