Absolute vs relative links in HTML

只谈情不闲聊 提交于 2019-12-08 10:52:44

问题


I have been looking for a complete definitive explanation of absolute vs relative links in HTML.

  1. <a href="http://www.example.com/page.html">link page</a>
  2. <a href="//page2.html">link page</a>
  3. <a href="/page3.html">link page</a>
  4. <a href="./page4.html">link page</a>
  5. <a href="../page5.html">link page</a>

So in the above list,

  1. is an absolute link
  2. is relative to site root with either http or https
  3. is relative to site root?
  4. is in the current directory?
  5. is up a directory?

回答1:


This is not HTML. This is absolute or relative paths in all aspects of a operating system.

  1. Yes, there's no interpretation. Is a link without SSL (https)
  2. Yes. It switches automatically to http if user visit the web in http, and https if user visit the web in https.
  3. Yes. It links to the //domain/thelink.ext
  4. Yes. As in a operating system, ./ is the current directory. Many times you must to specify this to ensure is the current.
  5. Yes. As in a operating system, ../ changes to parent directory. You can make nesting and ../../../ will mean that up 3 parent directories.



回答2:


  1. As you said, the absolute link for a unsecure connection (http://)
  2. Only links you to http://page2.html or https://page2.html depending on the current protocol
  3. Yes, links you to URL/page.html
  4. Yes, same as link -> Current directory
  5. Yes


来源:https://stackoverflow.com/questions/32460450/absolute-vs-relative-links-in-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!