Http://www.jb51.net/web/21806.html
Relative path-The directory path created based on the location of the page where the file is referenced. Therefore, when a Web page that is saved in a different directory references the same file, the path used will not be the same, so it is referred to as relative.
Absolute path-the directory path based on the Web site root directory. This is called absolute, meaning that when all Web pages reference the same file, the path used is the same.
In fact, the absolute path and the relative path are different, only in describing the directory path, the reference points used are different. Because the reference point of the root directory is the same for all files on the site, the path description using the root directory as the reference point is called an absolute path.
The following are some of the special symbols used to establish the path, and the meanings they represent.
"." -Represents the directory in which it is currently located.
".." --Represents a previous level of directory.
"/"--Represents the root directory.
Next, let's assume that the Web site that the reader builds has the directory path as shown.
If you want to reference the Beref.gif file in the ref.htm file, its relative path is as follows:
./subdir2/beref.gif
In the reference path above, "." Represents the current directory (DIR1), so "./subdir2" represents the SubDir2 in the current directory. In fact, the "./" can be omitted to be referenced directly in this way.
Subdir2/beref.gif
If you use an absolute path to refer to the file as a reference point in the root directory, the reference path is as follows:
/dir1/subdir2/beref.gif
If the structure of a Web site's directory is
What is the relative path to the reference beref.gif file?
If you want to reference the Beref.gif file in the ref.htm file, its relative path is as follows:
.. /subdir2/beref.gif
In the reference path above, "..." Represents the previous level of the directory, so/dir2 "represents the DIR2 subdirectory under the previous level directory. If you use an absolute path reference, the reference path is as follows:
/dir2/berer.gif
Let's take a more complicated example to compare the use of relative paths with absolute paths. Assume that you have a directory path like this in the Web site that the reader is building.
We use a table to illustrate the relative path and absolute path that should be used when a file references another file in the case.
| References by |
Quoted by |
Relative path |
Absolute path |
| Ref1.htm |
Beref1.gif |
.. /subdir2/beref1.gif |
/dir1/subdir2/beref1.gif |
| Ref2.htm |
Beref1.gif |
.. /.. /dir1/subdir2/beref1.gif |
/dir1/subdir2/beref1.gif |
| Ref1.htm |
Beref2.htm |
.. /.. /dir2/beref2.htm |
/dir2/beref2.htm |
| Ref2.htm |
Beref2.htm |
.. /beref2.htm |
/dir2/beref2.htm |
In the table above, the comparison needs to be described as ".. /.. /"represents the meaning.
".." Represents a previous level of directory, while the ".. /.. /"represents the previous level of the previous directory. Therefore, as can be seen from the table above, if the referenced file exists in the subdirectory of the current directory, or in another subdirectory of the previous level of the directory, the use of relative paths is relatively convenient. If not, then simply use the absolute path, but also more convenient. As you can see from the table above, the absolute path used by the reference file is the same when the same file is referenced.
Relative path web