|
To access the contents, click the chapter and section titles.
HTML 4.0 Sourcebook
Anchor Element: The NAME AttributeLinks to particular locations in a document, or between locations in the same document, are made possible through the NAME attribute of the A (anchor) element. The NAME attribute lets you assign a unique name, called a fragment identifier, to a particular place in a document. You can then link to this named location using a special form of URL that contains this special name. Figure 2.9 shows a document containing several named locations, illustrating one of the common uses of named locations to create a simple index for the contents of the page. The top of the page contains links that access the different sections of the document, while each section contains links that allow you to return to the top of the page and back to the contents listing. This page was originally developed by the author and Sian Meikle of the University of Toronto Library, and is part of a template document collection we distribute to university departments interested in developing their own document collections. The entire template is located at www.utoronto.ca/ian/Template/readme.html. Figure 2.9 A Typical HTML document, deptinfo.html, that contains named anchor elements. Portions of the document have been omitted to save space. Comments are in italics, while markers indicating the named anchors are in boldface italics. <HTML> <HEAD> <TITLE> Biology Department: General Information </TITLE> </HEAD> <BODY> <A HREF=depthome.html NAME=top><IMG SRC=home.gif ALT=[home]></A> <HR> <IMG ALIGN=right SRC=french2.gif ALT=[Picture of our Building]> <H1>Biology at the University of Toronto</H1> <ADDRESS> University of Toronto <BR> 150 St George Street, Room 213 <BR> Toronto Ontario M5S 1A1 CANADA <BR> <B>Tel:</B> (416)9787000 <BR> <B>Fax:</B> (416)9789000 <BR> <B>Email:</B> <a href=mailto:[email protected][email protected]</a> </ADDRESS> <HR> <B>On this page:</B> [<A HREF=#general>General Information</A>] [<A HREF=#facilities>Research Facilities</A>] [<A HREF=#history>Department History</A>] [< References Named Anchor] <HR> <H2><A NAME=general>General</A></H2> <BLOCKQUOTE> <P><EM> This example document ... [text deleted] ... There is one advantage, however, to keeping this material together it allows the user to print the entire document for reading away from the computer.</EM> </BLOCKQUOTE> <P> The University of Toronto is the largest university in Canada with 2500 graduate faculty and more than 9000 full and parttime graduate students. ... ... [textl deleted ...] <H2><A NAME=facilities>Research Facilities</a></H2> <P> The Department provides many facilities to aid astronomical research, and students and staff use national and international observatories all over the world ... ... [text deleted] [<a HREF=#top>... to top of page</A> <H2><A NAME=history>History of the Department</A> </H2> [<NAMED ANCHOR] <P> Biology became a major department in 1905. The first chair of the Department, Dr. Roland Fishburn, ... .......[more text deleted] [<a HREF=#top>... to top of page</A> <HR> <a HREF=depthome.html><IMG SRC=icons/home.gif ALT=[home]></A> </BODY> </HTML>
NAME Attribute and Fragment Identifiers There are four named anchors in Figure 2.9, but for this discussion we will focus on the one for the history section. The relevant hypertext anchor is: <A NAME=history>History of the Department</A> This anchor associates a fragment identifier, namely history, with this location in the document. Looking at Figure 2.10, you will see that this section of text is not rendered in any special way. In general, an anchor that contains only a NAME attribute is not specially displayed. From elsewhere in this document, this location can be referenced using a URL of the form <A HREF=#history>Department History</A> as illustrated in Figure 2.9. Note how the fragment identifier is indicated by prepending the hash character (#), to distinguish this from a regular URL. If the user clicks on the anchored text Department History, the browser will search for the named fragment identifier and scroll down (or up) the page to that location, as illustrated in Figure 2.11. NAME and HREF Combined A hypertext reference can simultaneously take both HREF and NAME attributes. An example is shown in the very first anchor in Figure 2.9, namely: <A HREF=depthome.html NAME=top><IMG SRC=home.gif ALT=[home]></A> Clicking on this anchor links the user to the document depthome.html (you will note in Figure 2.10, this image is highlighted as a linked hypertext anchor). At the same time, clicking on internal page links of the form <a HREF=#top>... to top of page</A> returns the user to the top of the page and to this same linked image. Thus, anchors can be both the start and destination of a hypertext link. Fragment Identifiers and Full URLs You can also access named locations from outside the document. This is done by appending the fragment identifier to the documents locator string. For example, if the full URL for the document deptinfo.html were http://www.utoronto.ca/ian/Template/deptinfo.html
then the URL that explicitly references the history section is simply (with the fragment identifier in boldface): <http://www.utoronto.ca/ian/Template/deptinfo.html#history> Figure 2.12 schematically illustrates some of the more common uses of named anchors. Details about writing valid fragment identifiers and URLs are found in Chapter 8. Lessons from Example 6
Exercises for Example 6Create a long HTML document (e.g., convert a word-processor document to HTML) and build some internal links between a contents list and the various sections. Note how much easier it is to read the document when these internal navigational tools are available.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |