Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

HTML 4.0 Sourcebook
(Publisher: John Wiley & Sons, Inc.)
Author(s): Ian S. Graham
ISBN: 0471257249
Publication Date: 04/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Relative URLs—Within an HTML Document

From within an HTML document, you do not always need to specify the full URL of a second resource. This is because being in a document implies knowledge of the current URL, which lets you reference neighboring documents or resources using a relative URL (also called a partial URL), which gives the location relative to that of the current document. To put it another way, the URL need only indicate the part of the URL different from that used to access the current document.

For example, suppose a user accesses the document file.html using the full URL

<http://www.stuff.edu/main/docs/file.html>

and that, within this document, there is a hypertext reference containing the relative URL:

<A HREF=“stuff.html”>anchor text</A>

Where is this file?Any information not present in a URL reference is considered to be the same as that used to access the current document. Thus, the partial URL stuff.html is transformed into a full URL by appropriating the required URL components from the URL that was used to access file.html. The completed URL is then:

http://www.stuff.edu/main/docs/stuff.html

which indicates, as expected, that stuff.html is on the same server and in the same directory as file.html. Other, equivalent, relative URLs would then be:

/main/docs/stuff.html
//www.stuff.edu/main/docs/stuff.html

The former appropriates http://www.stuff.edu from the current URL to complete the reference, while the latter appropriates only the http: part from the base URL of the current document.

You can also use relative URLs to reference resources at other locations on the same server. For example, from within the file file.html, the relative URL

../../main.html

references the file main.html in the root HTTP directory, namely:

http://www.stuff.edu/main.html

Note how, with relative URLs, the special symbol “..” indicates a location one up in the directory hierarchy, just as it does in DOS or UNIX.

Relative URLs are also discussed in Example 2 from Chapter 1.

Relative URLs and the BASE Element

Relative URLs are very useful when constructing large collections of documents that will be kept together. However, relative URLs become invalid for a single document that is moved to a new directory or a new Internet site. This problem can be mitigated by using a BASE element within the moved document. BASE records the base—or original—location of the document. Relative URLs are evaluated relative to this recorded base URL. Thus, when the document is moved, all relative URLs are determined relative to the URL recorded by BASE, and the linked resources are correctly accessed from the original server. For example, suppose a document contains the head-level element

<BASE HREF=“<http://www.flopsy.com/dir1/path/file2.html>”>

and also the body-level hypertext reference:

<A HREF=“../stuff.html”>link to some stuff</A>.

Then, this hypertext link references the URL

<http://www.flopsy.com/dir1/stuff.html>

regardless of the actual location of the document containing the reference, since the BASE always directs relative URLs to an address relative to the base location.

Fragment Identifiers

In some cases, you will see locator strings of the form:

<http://some.where.edu/Stuff/Path/plonk.html#location>

The portion of the URL following the hash (#) character is called a fragment identifier and references a particular location within the designated URL. Within the targeted HTML document, this location must be marked by an anchor element of the form:

<A NAME=“location”>text marker</A>

where the string location is the text string marking the location.

When a browser accesses a resource specified by a URL, it first strips off all characters following and including an unencoded hash (#) and uses the remaining string as the resource URL. Consequently, for the “stripped” URL to be valid, a fragment identifier must be the very last substring on a URL. The browser preserves the fragment identifier as local information and, after retrieving the indicated resource, looks for the indicated location. The browser will present the document to the user, such that the location is prominently displayed, either by placing the location at the top of the screen or by highlighting it in some way.

You can also reference named locations from within the document that contains the location. The general form for this is:

<A HREF=“#location”>anchor text</A>

That is, you reference just the fragment identifier, and nothing else. A browser should then scroll the document to prominently display the targeted location.

The use of fragment identifiers was discussed in detail in Example 6 of Chapter 2.

URL Specifications

We now look at the details of the different URL schemes. The protocols that can be referenced by URLs are listed in the Table 8.2; italicized entries indicate schemes that are not widely supported, while “Pseudo”-URLs lists URL schemes that are not formal resource locators, but that are commonly encountered in Web browsers and Web applications.


Previous Table of Contents Next


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.