html - 没有 http 协议的链接 [英] html - links without http protocol
问题描述
我们是否有理由在链接的 href
属性中包含 http
/https
协议?
Is there a reason we include the http
/ https
protocol on the href
attribute of links?
把它关掉可以吗:
<a href="example.com">my site</a>
推荐答案
包含http:"或https:"部分部分只是传统问题,部分是实际指定协议的问题.如果默认,则使用当前页面的协议;例如,//www.example.com 变为 http://www.example.com 或 https://www.example.com 取决于引用页面的 URL.如果一个网页保存在本地磁盘上然后从那里打开,它没有协议(只有文件:伪协议),所以像//www.example.com 这样的 URL 将不起作用;所以这是包含http:"或https:"部分的原因之一.
The inclusion of the "http:" or "https:" part is partly just a matter of tradition, partly a matter of actually specifying the protocol. If it is defaulted, the protocol of the current page is used; e.g., //www.example.com becomes http://www.example.com or https://www.example.com depending on the URL of the referring page. If a web page is saved on a local disk and then opened from there, it has no protocol (just the file: pseudo-protocol), so URLs like //www.example.com won’t work; so here’s one reason for including the "http:" or "https:" part.
省略//"部分是一个完全不同的问题,将 URL 转换为相对 URL,该 URL 将被解释为相对于当前基本 URL.
Omitting also the "//" part is a completely different issue altogether, turning the URL to a relative URL that will be interpreted as relative to the current base URL.
www.example.com 在浏览器地址行中输入或粘贴时能正常工作的原因是相对 URL 在那里没有意义(没有与之相关的基本 URL),因此浏览器供应商决定暗示http://" 前缀.
The reason why www.example.com works when typed or pasted on a browser’s address line is that relative URLs would not make sense there (there is no base URL to relate to), so browser vendors decided to imply the "http://" prefix there.
这篇关于html - 没有 http 协议的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!