Absolute URLs omitting the protocol (scheme) in order to preserve the one of the current page
Asked Answered
G

3

91

I saw the //somepage.com/resource url format. For example:

<img src="//remotesite.com/image1.jpg" />

The point of this is that if the current page (the page defining the img tag) is using http, then the request to the remote site is made via http. If it is https - it's https. This eliminates browser warnings of not fully encrypted pages.

My question is - is this URL format safe to use for all browsers. And is it a standard?

Galenism answered 12/2, 2011 at 13:15 Comment(4)
Related: Can I change all my http:// links to just // ? answers your first and Scheme relative URLs answers your second question.Arrington
Similar question: stackoverflow.com/questions/8343942/…Affidavit
possible duplicate of Is it valid to replace http:// with // in a <script src="http://...">?Pulverulent
Although you didn’t use the term in your question, I added the protocol-relative tag (so this question can be found easier).Costive
S
62

is this URL format safe to use for all browsers.

I can't say anything for sure, but you should be able to test it in different browsers.

And is it a standard?

Technically, it is called "network-path reference" according to RFC 3986. Here is the scheme for it:

  relative-ref  = relative-part [ "?" query ] [ "#" fragment ]

  relative-part = "//" authority path-abempty
                / path-absolute
                / path-noscheme
                / path-empty

There is a problem though, when used on a <link> or @import, IE7 and IE8 download the file.

Here is a post written by Paul Irish on the subject:

Susannasusannah answered 12/2, 2011 at 13:22 Comment(2)
Note that Paul Irish has updated his post to indicate that resources should now always be served over https when it is feasible to do so. See the post linked by @Susannasusannah above.Bandeen
Google analytics snippet code uses this scheme, so i bet it's cross browser while this tool is used by many many websites, they would just provide https version otherwiseScever
P
23

Yes I believe it is. Paul Irish coined the term Protocol Relative URL.

I'd also point out it's part of the HTML5Boilerplate project which evangelises cross browser compatibility.

Note: there is an edge case in IE6 with google analytics which is mentioned in Paul's article. So it's not perfect.

Provencher answered 12/2, 2011 at 13:22 Comment(1)
This is no longer true, see https://mcmap.net/q/18719/-is-it-valid-to-replace-http-with-in-a-lt-script-src-quot-http-quot-gt or here, they now use https://. Also see here why ommiting the protocol scheme might not be a good idea.Karmen
U
2

Should be safe.

Is specified as format to use in Google's HTML/CSS styleguide: EDIT: latest url : https://google.github.io/styleguide/htmlcssguide.xml#Protocol

Unconstitutional answered 11/8, 2015 at 14:23 Comment(3)
the styleguide has moved to google.github.io/styleguide/htmlcssguide.html#ProtocolBandsman
@ЕмилЦоков, have you considered updating the answer thus?Cavalry
it is updated nowBandsman

© 2022 - 2024 — McMap. All rights reserved.