|
|
||||||||||
|
|
Navigation | Relative Addresses |
|
|
|
||||||
Relative AddressesThe term "path" is sometimes confusing for new users of the Web. However, it is really a simple concept: A path is the sequence of directories that must be traversed to get from one directory to another. (In Windows directories are often called "folders"; we will use the two terms as synonymns.) In order to construct all but the simplest of Web documents, it is useful to know the basics of how to specify paths between directories and files.
The subsequent
material in this
section is somewhat more technical than the preceding discussion. It is not
essential to casual Web use, but is important if you plan to construct your own
Web site.
Directory Hierarchies
This typical directory structure has a master directory (usually called the root directory and designated on a PC by the symbol "\") that contains within it all other directories as subdirectories, and each directory so contained may itself contain subdirectories. In this example the root directory contains the directory Dir1, and this directory in turn contains the directories Dir2 and Dir5. Finally, Dir2 contains subdirectories Dir3 and Dir4, while Dir5 contains a subdirectory Dir6.
The files on the system are contained in these directories. In the figure some
representative
files are illustrated in red. For example, the file index.html
(see the green arrow) is
contained in the directory Dir2, which is a subdirectory of
Dir1, which is finally a subdirectory of the root directory.
Directory Trees
Paths
Specification of Paths
Thus, to reach file6.ext from index.html
we go back to Dir1, then to its subdirectory Dir5,
and then to its subdirectory Dir6.
The notation that is used to go back one level in a directory
hierarchy is "..\" (two dots, followed by a backslash),
and we may say that the location of file6.ext
relative to index.html is
..\Dir5\Dir6\file6.html, as illustrated in the preceding figure.
Absolute and Relative Addressing
Notice the difference between absolute and relative addresses: if I move the file index.html to a new directory, the address of file6.ext relative to index.html changes, but its absolute address remains the same. On the other hand, suppose I move the entire directory structure to the right of "Root" in the directory tree intact to a new location on the same computer or a different computer, as illustrated in the adjacent diagram. Then generally the absolute address of file6.ext would change, but its address relative to index.html (indeed, relative to any file in the directories that I moved) would stay the same.
Thus, in the preceding diagram the absolute address
of file6.ext is \Dir1\Dir5\Dir6\file6.html in the upper
example and \new\www\Dir1\Dir5\Dir6\file6.html in the lower example,
but since the part
inside the gray boxes is the same in the two cases, the relative address as
referenced from index.html is unchanged.
The Connection Between Paths and URLs
Thus to determine the appropriate path portion of a document's absolute
URL, one
needs to know its directory path on the server, relative to the
html document root. If the server is a Windows machine, it has a
path of the form that we discussed above. To get the path portion
of the document's absolute URL, simply:
To obtain the complete absolute URL, combine this path part with the host name and the string (http://, ftp://, etc.) that gives the method of file access. To determine the URL of document A relative to document B, just write the directory path of A relative to B and replace each backslash with a (forward) slash.
So why, you may well ask, do I have to worry about both slashes and
backslashes? Why couldn't we just use one or the other for both directory
paths and URLs? The answer lies in the fact that URLs and web browsers
were first developed on Unix machines. There the forward slash is used
in directory paths and they were naturally carried over into
URLs. Unfortunately, however, the backslash already had a long history
of serving as the delimiter in directory paths in the PC world, so
it was retained in this role even after Windows machines began to be used
as Web servers. Consequently, in the Windows environment one has to use
two different symbols, while you can get away with only one in the Unix
world.
Use Relative Addressing Whenever Possible
Homepage Exercise: Relative and Absolute Addresses
The elementary examples in this exercise illustrate the difference between absolute and relative addressing, and how to implement relative addressing for the two simple (but common) cases where the file being addressed is either in the same directory as the HTML file (the next.gif example), or in an immediate subdirectory of the HTML file (the gifs\previous.gif example). As we have noted above, it is generally advisable to use relative addressing in your Web pages to increase portability.
|
||||||||||
|
|
||||||||||