HTML 中带有主机的无协议绝对 URI?
我见过一些页面,这些页面看起来像是绝对URI,带有主机,但没有协议。例如:
<script src="//mc.yandex.ru/metrika/watch.js" type="text/javascript"></script>
我的假设是,这意味着“使用与我们现在使用的协议相同的协议”,因此父页面将请求 https://mc.yandex.ru/metrika/watch.js
如果它自己的协议是https
。
这个语法正确吗?标准的一部分?这是什么意思?
I have seen some pages that refer to what appear to be absolute URIs, with a host, but without a protocol. For example:
<script src="//mc.yandex.ru/metrika/watch.js" type="text/javascript"></script>
My assumption is that this means 'use the same protocol as what we are on now', so the parent page will request https://mc.yandex.ru/metrika/watch.js
if its own protocol is https
.
Is this syntax even correct? Part of a standard? What does it mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它称为“网络路径引用”。有关此内容的文档可以在 RFC 3986 中找到。具体参见4.2节:
第 5.4 节:
因此,以双斜杠开头的 URI 将进行转换以匹配基本 URI。据我所知,它的一种用途(事实上,这是我见过的唯一用途)是在使用 CDN 时(例如,通过 Google CDN 包含 jQuery 时)。 Google 在
http
协议上托管一个版本,在https
协议上托管另一个版本,使用此 URI 格式将导致加载正确的版本,无论您使用哪种协议使用。更新(刚刚找到并阅读这篇文章 )
看来,在整个页面中使用此 URI 格式可以防止 IE 中出现“此页面包含安全和非安全项目”错误。但是,值得注意的是,这种格式会导致通过
link
元素包含文件,或者@import
指令导致包含的文件被请求两次。所有其他资源(例如图像和锚点)应按预期工作。It's called a "network path reference". The documentation for this can be found in RFC 3986. Specifically, see section 4.2:
And section 5.4:
So a URI starting with a double slash is transformed to match the base URI. One use of this that I know of (in fact, the only use I've ever seen) is when using a CDN (for example, when including jQuery via the Google CDN). Google hosts a version on the
http
protocol, and another on thehttps
protocol, and using this URI format will cause the correct version to be loaded, no matter which protocol you are using.Update (having just found and read this article)
It appears that using this URI format throughout a page can prevent the "This Page Contains Both Secure and Non-Secure Items" error in IE. However, it's worth noting that this format causes files included via a
link
element, or an@import
directive cause the included file to be requested twice. All other resources (such as images and anchors) should work as expected.