简体   繁体   English

以两个斜杠开头的URI……它们的行为如何?

[英]URI starting with two slashes … how do they behave?

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. 根据RFC 2396(URI语法)和RFC 2616(HTTP 1.1),这些URI以两个斜杠开头是有效的,但是不幸的是RFC并没有真正解释它们。

Can anyone point me to a resource which explains how browsers will/should/do process these URIs? 谁能指出我的资源来解释浏览器将/应该/如何处理这些URI?

The resource you're looking for is the RFC 3986 . 您正在寻找的资源是RFC 3986

See Section 4.2 and Section 5.4. 请参阅第4.2节和第5.4节。 Quoting from the latter: 引用后者:

Reference Resolution Examples 参考分辨率示例

Within a representation with a well defined base URI of: 在具有良好定义的基本URI的表示中:

    http://a/b/c/d;p?q

a relative reference is transformed to its target URI as follows: 相对引用将转换为其目标URI,如下所示:

  "g:h" = "g:h" "g" = "http://a/b/c/g" "./g" = "http://a/b/c/g" "g/" = "http://a/b/c/g/" "/g" = "http://a/g" "//g" = "http://g" "?y" = "http://a/b/c/d;p?y" "g?y" = "http://a/b/c/g?y" "#s" = "http://a/b/c/d;p?q#s" "g#s" = "http://a/b/c/g#s" "g?y#s" = "http://a/b/c/g?y#s" ";x" = "http://a/b/c/;x" "g;x" = "http://a/b/c/g;x" "g;x?y#s" = "http://a/b/c/g;x?y#s" "" = "http://a/b/c/d;p?q" "." = "http://a/b/c/" "./" = "http://a/b/c/" ".." = "http://a/b/" "../" = "http://a/b/" "../g" = "http://a/b/g" "../.." = "http://a/" "../../" = "http://a/" "../../g" = "http://a/g" 

This means that when the base URI is http://a/b/c/d;p?q and you use //g , the relative reference is transformed to http://g . 这意味着当基本URI为http://a/b/c/d;p?q且您使用//g ,相对引用将转换为http://g

These are protocol relative URLs. 这些是协议相关的URL。 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 and https resources on the same HTTPS page). 此符号通常用于避免“混合内容”问题(IE警告消息抱怨同一HTTPS页面上的httphttps资源)。

Update: Official documentation in RFC 3986: 更新: 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. 不以斜杠字符开头的相对参考称为相对路径参考。

They are protocol independent urls. 它们是协议无关的URL。 If the web page is served on https then the request uses https, if http then http. 如果网页在https上提供,则请求使用https,如果使用http,则使用http。

Paul Irish seems to have popularized them by including it in his boilerplate code. 保罗·爱尔兰(Paul Irish)似乎已通过将其包含在样板代码中来使其普及。

Be aware of that it is not only http or https independent, but also file , ftp , etc. 请注意,它不仅独立于httphttps ,而且还独立于fileftp等。

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. 这意味着,如果直接在本地主机上的浏览器中打开.htm文件, 浏览器将解析//作为文件协议,并且您的页面将无法工作。 It may cause problems in packed websites as "native" app using tools like Electron, PhoneGap, etc. 使用诸如Electron,PhoneGap等工具将其打包为“本机”应用程序时,可能会导致网站出现问题。

Example: 例:

<script src="//mywebsite.com/resource.js"></script>

to

<script src="file://mywebsite.com/resource.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM