开发者社区 问答 正文

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对所有的//都会忽略主机名,同上述的第二种情况.服务器上测试也是如此)

展开
收起
杨冬芳 2016-06-20 16:18:43 3035 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • IT从业

    :///;?#

    each of which, except , 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

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

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

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

    //segmentfault.com/q/1010000004282189
    screenshot

    2019-07-17 19:44:19 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论