URI starting with two slashes ... how do they behave?
Asked Answered
L

4

95

Lately I saw working code-blocks like this:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

And according to RFC 2396 (URI Syntax) and RFC 2616 (HTTP 1.1) these URI starting with two slashes are valid, but unfortunately the RFCs don't really explain them.

Can anyone point me to a resource which explains how browsers will/should/do process these URIs?

Lauro answered 1/11, 2010 at 16:56 Comment(0)
K
88

The resource you're looking for is the RFC 3986.

See Section 4.2 and Section 5.4. Quoting from the latter:

Reference Resolution Examples

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://a/b/c/g"
  "./g"           =  "http://a/b/c/g"
  "g/"            =  "http://a/b/c/g/"
  "/g"            =  "http://a/g"
  "//g"           =  "http://g"
  "?y"            =  "http://a/b/c/d;p?y"
  "g?y"           =  "http://a/b/c/g?y"
  "#s"            =  "http://a/b/c/d;p?q#s"
  "g#s"           =  "http://a/b/c/g#s"
  "g?y#s"         =  "http://a/b/c/g?y#s"
  ";x"            =  "http://a/b/c/;x"
  "g;x"           =  "http://a/b/c/g;x"
  "g;x?y#s"       =  "http://a/b/c/g;x?y#s"
  ""              =  "http://a/b/c/d;p?q"
  "."             =  "http://a/b/c/"
  "./"            =  "http://a/b/c/"
  ".."            =  "http://a/b/"
  "../"           =  "http://a/b/"
  "../g"          =  "http://a/b/g"
  "../.."         =  "http://a/"
  "../../"        =  "http://a/"
  "../../g"       =  "http://a/g"

This means that when the base URI is http://a/b/c/d;p?q and you use //g, the relative reference is transformed to http://g.

Kirby answered 1/11, 2010 at 17:3 Comment(5)
so can this be a solution to using javascript to determine http or https this way whatever it is it will workMatrilateral
Since you need to reach the parent level, use ../g — it will use the current protocol and lead to http://a/b/c/g.Burnet
It may be a novice question, but where does this base URI come from? the browser url bar? the server? a tag in the html page?Seldon
@thenaglecode: the answer is, it depends. In XML/XHTML/HTML5 you can use xml:base to set it explicitly on any element. By default in HTML the base URI is the same URI of the page (but inside a CSS file, the base URI is relative to the CSS uri, not the containing HTML, but older IE made it relative to the HTML). In other protocols and languages, it can be different (in XSLT, it is dependent on the current item, for instance). See also <html:base>.Monolatry
Important - the result is not always http! It depends on the protocol of the page context. If the context was loaded under http://a/b/c/d;p?q, then //g indeed resolves to http://g. But if the page was loaded as http://a/b/c/d;p?q then the result will be https://g. And also do not forget about other protocols like file:// (page is loaded from disk) - which will definitely give you a lot of headache.Viperous
M
64

These are protocol relative URLs. They point to an address, keeping the current protocol.

This notation is often used to avoid the "mixed content" problem (a IE warning message complaining about http and https resources on the same HTTPS page).

Update: Official documentation in RFC 3986:

A relative reference that begins with two slash characters is termed a network-path reference; such references are rarely used. A relative reference that begins with a single slash character is termed an absolute-path reference. A relative reference that does not begin with a slash character is termed a relative-path reference.

Minestrone answered 1/11, 2010 at 16:58 Comment(4)
Great to know, but how compliant is this with popular browsers. A quick search told me it doesn't work with IE6...is this an HTML5 feature??Angelesangelfish
@Shane this should work in all browsers. Do you have a link claiming it doesn't work in IE6?Minestrone
Pretty sure that one's an IE1 feature!Thyestes
plus for mentioning protocol relativeProhibition
C
30

They are protocol independent urls. If the web page is served on https then the request uses https, if http then http.

Paul Irish seems to have popularized them by including it in his boilerplate code.

Cathicathie answered 1/11, 2010 at 16:58 Comment(0)
H
2

Be aware of that it is not only http or https independent, but also file, ftp, etc.

It means if you open .htm file directly in your browser on localhost, browser will resolve // as file protocol and your page won't work. It may cause problems in packed websites as "native" app using tools like Electron, PhoneGap, etc.

Example:

<script src="//mywebsite.com/resource.js"></script>

to

<script src="file://mywebsite.com/resource.js"></script>
Hennebery answered 8/3, 2017 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.