11

Possible Duplicate:
Is it valid to replace with // in a <script src=“…”>?
Absolute URLs omitting the protocol (scheme) in order to preserve the one of the current page
Does using //www.example.com in Javascript chose http/https protocol automatically

I'm looking at some sample code from facebook and I see:

<script src="//connect.facebook.net/en_US/all.js"></script>

they use // instead of http:// -- is this something fancy that I don't know about yet?

Community
  • 1
  • 1
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
  • 5
    I swear this is a duplicate of a very well-known question with extreme votes, but I understand if you searched and couldn't find the original question because neither can I. – BoltClock Oct 07 '11 at 22:55
  • 1
    This post answers the question http://stackoverflow.com/questions/3583103/scheme-relative-urls , but if you don't know to search for *scheme relative URL*, you wouldn't find it – Ruan Mendes Oct 07 '11 at 22:57
  • @BoltClock I know the exact question question you're talking about, but I can't find it either. – please delete me Oct 07 '11 at 23:01
  • @JuanMendes: Well, typing "javascript" plus the title of this question brought me [to this answer](http://stackoverflow.com/questions/5799577/does-using-www-example-com-in-javascript-chose-http-https-protocol-automaticall). – user113716 Oct 07 '11 at 23:09
  • 1
    @Ӫ_._Ӫ: Even though this question really has nothing to do with JS... – Ruan Mendes Oct 07 '11 at 23:11
  • @JuanMendes: Heh, good point. Still considering that the question is tagged with "javascript", I didn't supply any information that wasn't used by the asker. – user113716 Oct 07 '11 at 23:14
  • @all I was leery about typing it as "Javascript" because it's technically in an html tag. However, since it's ended up as a protocol answer then either both or neither tag is correct. In any case, I didn't know what this was called or what it did, but I do now. – Jacksonkr Oct 08 '11 at 02:14
  • The very well-known question that other commenters alluded to may be http://stackoverflow.com/questions/550038/is-it-valid-to-replace-http-with-in-a-script-src-http. – John Pick Mar 27 '12 at 22:20
  • @Boltclock: Was it this one?: [Is it valid to replace http:// with // in a – Wesley Murch Mar 27 '12 at 22:47
  • @Madmartigan: That's the one, thanks! – BoltClock Mar 27 '12 at 23:13

3 Answers3

18

It's called a "protocol-relative URL". Similar to how a url starting with "/" is relative to the root of the current domain, a URL starting with "//" will link to the specified host and path, but using whatever protocol the current page was loaded using.

There's a nice description of them, and why they're useful, on the Wikimedia blog:

http://blog.wikimedia.org/2011/07/19/protocol-relative-urls-enabled-on-test-wikipedia-org/

2

Basically it gives you the ability to spit out one URL and have it use whatever protocol is currently being used.

Facebook probably uses the same HTML code regardless of whether the user is on HTTP or HTTPS. It's a way to fully qualify the domain without specifying the protocol.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
0

It's another type of relative URL, it uses the same protocol that the page is on.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217