Lecture 8: Linking

Overview:

  1. The Anchor Tag
  2. Absolute Links
  3. Relative Links
  4. Traversing Directory Structures
  5. Creating Within-Page Links
  6. Creating Mailto: Links
  7. Adding Titles
  8. Targeting Links

The Anchor Tag

Attribute Usage and Effect Values Accepted Default Deprecated?
Note: Additional attributes exist, but are rarely used.
accesskey Keyboard shortcut to select the anchor (the enter key or space bar would then activate the link). Used to improve accessibility, but in practice is seldom specified because of the time involved to code these and the difficulty of conveying these to the user.

In Windows press the 'alt' key (on a Mac use the 'ctrl' key) plus the indicated letter to move the focus to the indicated link.

Be sure to specify each accesskey letter only once.
A single character   Not deprecated
href By far the most common use of anchors is to link together documents via these href (hypertext reference) values; they are also used to launch email programs. Document address (either absolute or relative) or mailto: and email address.   Not deprecated
name This attribute is used to place a within-page anchor. Name of anchor (your choice; these should be unique and should not be the same as an id value; avoid spaces in value)   Not deprecated
rel This is a forward link. It specifies the relationship between the current document and the document being linked to; these are not used very frequently.

They could assist search engine spiders in indexing content, assist by preloading the next document (in a sequence), or otherwise provide meaning.
Mouse over the value for details:

start | next | prev | previous | contents | toc | index | glossary | copyright | chapter | section | subsection | appendix | help | bookmark
  Not deprecated
rev This is a reverse link. It is just like rel, just in the opposite direction. Again, not used very frequently.

They could assist search engine spiders in indexing content or otherwise provide meaning.
Mouse over the value for details:

start | next | prev | previous | contents | toc | index | glossary | copyright | chapter | section | subsection | appendix | help | bookmark
  Not deprecated
tabindex Determines the tabbing sequence for objects on the page. Number from 0 to 32767; leading zeros are usually ignored.   Not deprecated
target Allows you to specify the browser window where the linked document will open; used primarily for frames-based layouts.

Can be used to open new browser windows, although this has been deprecated in favor of using JavaScript to open new windows.
Name of window

"_blank" to open a new window

Some other pre-determined values are also available (covered in INP 170)
  Deprecated

Absolute Links

Relative Links

Traversing Directory Structures

To provide additional examples of relative linking, the following diagram has been created showing a directory structure:

Directory Structure Example

Creating Within-Page Links

Sample code for Approach 1:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Within-Page Links (Approach 1)</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <p>
    On This Page:<br />
    <a href="#section1">Section 1</a><br />
    <a href="#section2">Section 2</a><br />
    <a href="#section3">Section 3</a>
    </p>

    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

    <h1><a name="section1">Section 1</a></h1>

    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

    <h1><a name="section2">Section 2</a></h1>

    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

    <h1><a name="section3">Section 3</a></h1>
</body>
</html>

See how this example renders

Sample code for Approach 2:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Within-Page Links (Approach 2)</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <p>
    On This Page:<br />
    <a href="#section1">Section 1</a><br />
    <a href="#section2">Section 2</a><br />
    <a href="#section3">Section 3</a>
    </p>

    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

    <h1 id="section1">Section 1</h1>

    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

    <h1 id="section2">Section 2</h1>

    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

    <h1 id="section3">Section 3</h1>
</body>
</html>

See how this example renders

Creating Mailto: Links

Adding Titles

Targeting Links