chrome是如何解析链接中的URL路径的

准备搭建GitHub-pages发现一个有趣的问题:
通过jkeyll生成静态页面,本地测试时发现,由于_config.yml 中设置 baseUrl = /,生成的index.html只要包含链接,href即变成//开头。chrome似乎对于//开头的路径有特殊的解析规则(本地环境localhost):

 <li><a href="/2016/01/10/first-posts.html">my first post</a></li>

http://localhost/index.html中点击时,会自动跳转为

http://0.0.7.224/01/10/first-posts.html

直接打开first-posts页面,其中存在返回链接:

<a href="//index.html">Go back</a>

点击时跳转成

http://index.html/

稍微测试了一下。"//1"代表"0.0.0.1"的保留地址,以此向上类推,似乎是chrome有意为之,但是safari却把"//"按"/"处理,所以浏览器点击链接时无异常。
chrome这种设定有什么说法么,似乎没有看到相关资料。
(有误,safari对所有的//都会忽略主机名,同上述的第二种情况.服务器上测试也是如此)

阅读 6.2k
1 个回答
     <scheme>://<net_loc>/<path>;<params>?<query>#<fragment>

   each of which, except <scheme>, may be absent from a particular URL.
   These components are defined as follows (a complete BNF is provided
   in Section 2.2):

      scheme ":"   ::= scheme name, as per Section 2.1 of RFC 1738 [2].

      "//" net_loc ::= network location and login information, as per
                       Section 3.1 of RFC 1738 [2].

      "/" path     ::= URL path, as per Section 3.1 of RFC 1738 [2].

      ";" params   ::= object parameters (e.g., ";type=a" as in
                       Section 3.2.2 of RFC 1738 [2]).

      "?" query    ::= query information, as per Section 3.3 of
                       RFC 1738 [2].

      "#" fragment ::= fragment identifier.

: 之前是 scheme
// 以后 / 之前是 net_loc 主机地址

比如当前页面有个链接 //segmentfault.com/q/1010000004282189

segmentfault.com 主机下面的 /q/1010000004282189
http://segmentfault.com/q/1010000004282189

而不是 http://segmentfault.com/segmentfault.com/q/1010000004282189

这种写法一般还用于同时解决httphttps的问题,省略了'http:' 'https:' 浏览器根据当前协议处理 url

你在本地页面有这个链接 那么结果就是 file://segmentfault.com/q/1010000004282189

http://www.w3.org/Addressing/rfc1808.txt

<a href="//segmentfault.com/q/1010000004282189">//segmentfault.com/q/1010000004282189</a>

本地访问file://

localhost http://

ie

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题