javascript - chrome是如何解析链接中的URL路径的
迷茫
迷茫 2017-04-10 16:47:57
[JavaScript讨论组]

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

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(1)
PHP中文网
     <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>

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号