问题
I have been looking for a complete definitive explanation of absolute vs relative links in HTML.
<a href="http://www.example.com/page.html">link page</a>
<a href="//page2.html">link page</a>
<a href="/page3.html">link page</a>
<a href="./page4.html">link page</a>
<a href="../page5.html">link page</a>
So in the above list,
- is an absolute link
- is relative to site root with either http or https
- is relative to site root?
- is in the current directory?
- is up a directory?
回答1:
This is not HTML. This is absolute or relative paths in all aspects of a operating system.
- Yes, there's no interpretation. Is a link without SSL (https)
- Yes. It switches automatically to http if user visit the web in http, and https if user visit the web in https.
- Yes. It links to the
//domain/thelink.ext
- Yes. As in a operating system,
./
is the current directory. Many times you must to specify this to ensure is the current. - Yes. As in a operating system,
../
changes to parent directory. You can make nesting and../../../
will mean that up 3 parent directories.
回答2:
- As you said, the absolute link for a unsecure connection (http://)
- Only links you to http://page2.html or https://page2.html depending on the current protocol
- Yes, links you to URL/page.html
- Yes, same as link -> Current directory
- Yes
来源:https://stackoverflow.com/questions/32460450/absolute-vs-relative-links-in-html