开发者

Protocol-less absolute URIs, with host, in HTML?

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/javascri开发者_开发百科pt"></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?


It's called a "network path reference". The documentation for this can be found in RFC 3986. Specifically, see section 4.2:

A relative reference that begins with two slash characters is termed
a network-path reference; such references are rarely used.

And section 5.4:

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://g"
...

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 the https 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜