以两个斜杠开头的 URI...它们的行为如何?
最近我看到了这样的工作代码块:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
根据 RFC 2396(URI 语法)和 RFC 2616(HTTP 1.1),这些以两个斜杠开头的 URI 是有效的,但不幸的是 RFC 并不真正有效解释一下。
谁能给我指出一个资源来解释浏览器将/应该/如何处理这些 URI?
Lately I saw working code-blocks like this:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
And according to RFC 2396 (URI Syntax) and RFC 2616 (HTTP 1.1) these URI starting with two slashes are valid, but unfortunately the RFCs don't really explain them.
Can anyone point me to a resource which explains how browsers will/should/do process these URIs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您要查找的资源是 RFC 3986。
参见第 4.2 节和第 5.4 节。引用后者:
这意味着当基本 URI 为
http://a/b/c/d;p?q
并且您使用// g
,相对引用转换为http://g
。The resource you're looking for is the RFC 3986.
See Section 4.2 and Section 5.4. Quoting from the latter:
This means that when the base URI is
http://a/b/c/d;p?q
and you use//g
, the relative reference is transformed tohttp://g
.这些是协议相对 URL。 它们指向一个地址,保留当前的地址协议。
此表示法通常用于避免“混合内容”问题(IE 警告消息抱怨同一 HTTPS 页面上的
http
和https
资源)。更新:RFC 3986 中的官方文档 :
以两个斜杠字符开头的相对引用称为
网络路径参考;很少使用此类参考文献。一个
以单个斜杠字符开头的相对引用是
称为绝对路径引用。一个相对参考
不以斜杠字符开头的称为相对路径引用。
These are protocol relative URLs. They point to an address, keeping the current protocol.
This notation is often used to avoid the "mixed content" problem (a IE warning message complaining about
http
andhttps
resources on the same HTTPS page).Update: Official documentation in RFC 3986:
A relative reference that begins with two slash characters is termed
a network-path reference; such references are rarely used. A
relative reference that begins with a single slash character is
termed an absolute-path reference. A relative reference that does
not begin with a slash character is termed a relative-path reference.
它们是独立于协议的 url。如果网页通过 https 提供,则请求使用 https,如果是 http,则使用 http。
Paul Irish 似乎通过将其包含在他的样板代码中来普及它们。
They are protocol independent urls. If the web page is served on https then the request uses https, if http then http.
Paul Irish seems to have popularized them by including it in his boilerplate code.
请注意,它不仅与 http 或 https 无关,而且还与 file、ftp 等
无关 。意味着如果您直接在本地主机上的浏览器中打开.htm文件,浏览器会将//解析为文件协议并且您的页面将无法工作。 Electron、PhoneGap 等工具作为“本机”应用程序打包的网站可能会出现问题。
示例:
使用
Be aware of that it is not only http or https independent, but also file, ftp, etc.
It means if you open .htm file directly in your browser on localhost, browser will resolve // as file protocol and your page won't work. It may cause problems in packed websites as "native" app using tools like Electron, PhoneGap, etc.
Example:
to