4

Possible Duplicate:
Can I change all my links to just //?

I came across this recently whilst poking around the markup for Google's 404 page. In it, they use // at the start of their URLs, in anchors in the markup, as well as referencing images/etc in their CSS.

Since it's not something I've come across before, and given that Google is notorious at finite-detail optimisation, I thought I'd ask here and see if anyone has any more information on the use of // instead of http:// it's not something that can be easily Googled about to find an answer.

I'm aware that // is useful in applications where either http or https might come into play, but apart from that, are there other benefits? Is it supported by all browsers? Are there any use limitations? Is it new/old?

I'd be really interested if anyone has any information.

Community
  • 1
  • 1
johnkavanagh
  • 4,614
  • 2
  • 25
  • 37
  • `"I'm aware that // is useful in applications where either http or https might come into play, but apart from that, are there other benefits?"` - I guess everyone read over that part? – thirtydot Mar 03 '11 at 21:58
  • well, the main reason is to avoid mixed-content warnings that a browser would produce if you loaded the page through https but other resources via plain http. – Jesse Cohen Mar 03 '11 at 22:04

3 Answers3

13

// is "Whatever the current protocol is".

On a page served over http, it is http://, and over https it is https://.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I learned something new today. – Jonathon Faust Mar 03 '11 at 21:54
  • As did I. I've only ever used `/` and, come to think of it, knowing that would have made life ever so slightly simpler in the past. Maybe. – Guttsy Mar 03 '11 at 21:57
  • What to use if we actually want to point to `//`, for example `http://www.example.com//`? –  Mar 03 '11 at 21:57
  • Thanks for your answer David, that's what I assumed. So where can // be used reliably? In anchors? In CSS src declarations? In link tags? Is it literally just interchangeable with any absolute URL use? What about browser compatibility? – johnkavanagh Mar 03 '11 at 22:09
  • Ideally not with CSS. See my answer. Also, see [here](http://stackoverflow.com/questions/4831741/can-i-change-all-my-http-links-to-just) and [here](http://stackoverflow.com/questions/3583103/scheme-relative-urls). – thirtydot Mar 03 '11 at 22:13
6

The only downside I could find is this:

http://www.stevesouders.com/blog/2010/02/10/5a-missing-schema-double-download/

Internet Explorer 7 & 8 will download stylesheets twice if the http(s) protocol is missing.

..

A protocol relative URL doesn’t contain a protocol. For example,

http://stevesouders.com/images/book-84x110.jpg
becomes
//stevesouders.com/images/book-84x110.jpg

Browsers substitute the protocol of the page itself for the resource’s missing protocol.

..

However, if you try this in Internet Explorer 7 and 8 you’ll see that stylesheets specified with a protocol relative URL are downloaded twice. Hard to believe, but true.

I just tested it with Wireshark and IE8, and it's true.

So, if you care about having a performant website, avoid using // with CSS.

thirtydot
  • 224,678
  • 48
  • 389
  • 349
  • 1
    Also not good for HTML email. Causes Outlook to choke. http://blog.feedblitz.com/2009/06/outlook-hangs-opening-emails-solved.html – Bryan Downing Mar 03 '11 at 22:12
3

// is a protocol-less url. it will use http or https depending on the browser's connection.

There is a good explanatory article of why you would want to do this here: http://encosia.com/2011/01/19/cripple-the-google-cdns-caching-with-a-single-character/

Jesse Cohen
  • 4,010
  • 22
  • 25