Relative and absolute paths

1. Absolute and relative paths

1.  Absolute path: It is the real path of your file or directory on the hard disk. For example , the image " bg.jpg " is stored in the " E:\img " directory of the hard disk, then the absolute path of the image " bg.jpg " The path is " E:\img\bg.jpg"

2.  Relative path: As the name implies, a relative path is a path relative to the current file. This method is generally used to indicate paths in web pages. For example, the "bg.jpg" picture is referenced in the "s1.html" file , because the " bg.jpg " picture is in the same directory relative to " s1.html ". If the following code is used in the " s1.html " file, as long as the relative positions of the two files have not changed ( that is to say, they are still in the same directory ) , no matter where they are uploaded to the Web server, The images are displayed correctly in the browser. For another example, suppose the directory where the " s1.html " file is located is under " E:\html ", and the directory where the " bg.jpg " picture is located is " E:\html\img ", then the " bg.jpg " picture is relatively For the " s1.html " file, it is in the " img " subdirectory of the directory where it is located, the path to reference the image should be: img/bg.jpg 

3. Advantages and disadvantages of relative paths and absolute paths

(1) Advantages of absolute paths

①If  someone saves your web page to another computer, the links, pictures, css , and js in it will still be linked to the path of your computer. At this time, the resource cannot be found and cannot be loaded, which can prevent plagiarized

②If  the location of the web page changes, the link inside still points to the correct URL

(2) Disadvantages of absolute paths

① It is inconvenient to use absolute paths when coding, because the link should point to the real domain name, not the development site

The pros and cons of relative paths are almost opposite to the pros and cons of absolute paths

(3) Advantages of relative paths

① It is easy to move the content, and the entire directory can be moved

② The test method is more flexible, and it is more convenient to test locally

(4) Disadvantages of relative paths

① When the location of some content pages is changed, the link is easy to fail

② It is easy to be collected and plagiarized by people in a large area

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326690710&siteId=291194637