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

发布于 2024-09-30 00:01:03 字数 314 浏览 5 评论 0原文

最近我看到了这样的工作代码块:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌酱 2024-10-07 00:01:03

您要查找的资源是 RFC 3986

参见第 4.2 节和第 5.4 节。引用后者:

参考解析示例

在具有明确定义的基本 URI 的表示中:

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

相对引用将转换为其目标 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”

这意味着当基本 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:

Reference Resolution Examples

Within a representation with a well defined base URI of:

    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"

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.

白衬杉格子梦 2024-10-07 00:01:03

这些是协议相对 URL。 它们指向一个地址,保留当前的地址协议。

此表示法通常用于避免“混合内容”问题(IE 警告消息抱怨同一 HTTPS 页面上的 httphttps 资源)。

更新: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 and https 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.

木緿 2024-10-07 00:01:03

它们是独立于协议的 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.

肤浅与狂妄 2024-10-07 00:01:03

请注意,它不仅与 httphttps 无关,而且还与 fileftp

无关 。意味着如果您直接在本地主机上的浏览器中打开.htm文件,浏览器会将//解析为文件协议并且您的页面将无法工作。 Electron、PhoneGap 等工具作为“本机”应用程序打包的网站可能会出现问题。

示例:

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

使用

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

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:

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

to

<script src="file://mywebsite.com/resource.js"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文