ptg16476052
Linking to Specific Places Within Documents 105
▼ 6
For example, to create an anchor at the section of a page labeled Part 4, you might add an
ID part4 to the heading, similar to the following:
Part Four: Grapefruit from Heaven
To point to an anchor in a link, use the same form of link that you would when linking
to the whole page, with the filename or URL of the page in the href attribute. After the
name of the page, however, include a hash sign (#) and the ID of the element exactly as
it appears in the id attribute of that element (including the same uppercase and lowercase
characters!), as follows:
This link tells the browser to load the page mybigdoc.html and then to scroll down to the
anchor named part4. The text inside the anchor definition will appear at the top of the
screen.
The name Attribute of the Tag
Before browsers supported linking to elements directly using their IDs, you had to use
the name attribute of the tag to create anchors on the page to which you could link.
Rather than including the href attribute in your tag to link to a location, you included
the name attribute to indicate that the was an anchor to which someone could link.
For example, you would write the previous example as follows:
Part Four: Grapefruit from Heaven
The tag wouldn’t produce a visible change on the page, but it would provide an anchor to
which you could link. Best practices recommend that you avoid using the name attribute
and use the ID attribute instead. You can use the ID attribute on any HTML element, not
just the tag. However, you may still encounter old markup that uses the tag in
this way.
Exercise 6.4: Linking Sections Between Two Pages
Now let’s create an example with two pages. These two pages are part of an online ref-
erence to classical music, in which each web page contains all the references for a par-
ticular letter of the alphabet (a.html, b.html, and so on). The reference could have been
organized such that each section is its own page. Organizing it that way, however, would
have involved several pages to manage, as well as many pages the readers would have to
load if they were exploring the reference. Bunching the related sections together under
lettered groupings is more efficient in this case. ▼