Html URI 以两个斜杠开头……它们的行为如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4071117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URI starting with two slashes ... how do they behave?
提问by pagid
Lately I saw workingcode-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 的资源吗?
采纳答案by Daniel Vassallo
The resource you're looking for is the RFC 3986.
您正在寻找的资源是RFC 3986。
See Section 4.2 and Section 5.4. Quoting from the latter:
请参阅第 4.2 节和第 5.4 节。引用后者:
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:
"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"
一个相对引用被转换为它的目标 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
。
回答by Pekka
These are protocol relative URLs.They point to an address, keeping the current protocol.
这些是协议相对 URL。它们指向一个地址,保持当前协议。
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警告消息抱怨http
和https
资源相同的HTTPS页)。
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.
以两个斜杠字符开头的相对引用称为网络路径引用;此类引用很少使用。以单个斜杠字符开头的相对引用称为绝对路径引用。不以斜杠字符开头的相对引用称为相对路径引用。
回答by meder omuraliev
They are protocol independent urls. If the web page is served on https then the request uses https, if http then http.
它们是独立于协议的 url。如果网页在 https 上提供,则请求使用 https,如果是 http,则使用 http。
Paul Irish seems to have popularized them by including it in his boilerplate code.
Paul Irish 似乎通过将它们包含在他的样板代码中来普及它们。
回答by lukyer
Be aware of that it is not only httpor httpsindependent, but also file, ftp, etc.
请注意,它不仅独立于http或https,还独立于file、ftp等。
It means if you open .htmfile directly in your browser on localhost, browser will resolve //as file protocoland your page won't work. It may cause problems in packed websites as "native" app using tools like Electron, PhoneGap, etc.
这意味着如果您在本地主机上的浏览器中直接打开.htm文件,浏览器会将//解析为文件协议,并且您的页面将无法工作。它可能会在使用 Electron、PhoneGap 等工具作为“本机”应用程序打包的网站中导致问题。
Example:
例子:
<script src="//mywebsite.com/resource.js"></script>
to
到
<script src="file://mywebsite.com/resource.js"></script>