tags:

views:

114

answers:

5

I've seen some sites use //somedomain.com/images/img.jpg vs using http://somedomain.com/images/img.jpg which includes the http: as well.

Is there a difference between the two? Is the browser just correcting the missing http: and these people are being lazy? Im curious behind the reasoning.

+8  A: 

If you are already on the site via http, it will assume you are talking about http and connect to the same server via the same protocol. Same with https. If you are on http and want to go to https, you would need to specify the protocol in the href.

rakuo15
so this would be used on sites that can either be viewed on `http` or `https`?
chadley
@chadley - the intention is less for local use, but rather remote use. If `superanalytics.com` offers both HTTP and HTTPS, it might tell you to include its analytics script with the URI `//superanalytics.com/analytics.js`. That way, if you're running HTTP, SuperAnalytics will, too. If you're running HTTPS, SuperAnalytics will, too. And then you don't get those nasty "This is a secure site with unsecure things on it!" warnings.
Matchu
thank you for clarifying matchu. that makes perfect sense now that you've pointed out an example.
chadley
A: 

I've never seen //somedomain.com/images/img.jpg and don't think it's legal.

I have seen /images/img.jpg which means "use the current domain". It's helpful if you have several web address (e.g., aaaaa.com & aaaaa.net) pointing to the same site.

James Curran
@James: It is legal in fact.
Daniel Vassallo
+8  A: 

Not that I've seen it used before, but it is a valid URI-reference. From the grammar:

URI-reference = URI | relative-ref

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

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

where as an absolute URI is:

   URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

For more information, feel free to read the rest of RFC3986

Mic
+1. It's excellent! I will use it now everywhere! (At least for the links to the files on the server that hosts static content).
MainMa
Note that this means that there's no reason to use two slashes in practice — just one will count as an absolute path.
Chuck
I think this answer misses the point. It's valid, but also functional. See @rakuo15's answer.
Matchu
Or perhaps this answer encourages research and self-study, by providing a teaser and a link? :)
Mic
@Chuck: it makes sense to use two slashes. Let's say you have http://www.my-dynamic-website.com/some-path/Default.aspx and all static content is hosted at http://my-static-content.com/. Omitting *http* to link to the second domain will save some space in this case.
MainMa
this answer is good for proving the use of it is ok but @rakou15's does fully answer my question. Thanks!
chadley
@MainMa: it will save space, but when someone saves a page and access it using the `file` protocol, things will appear to be broken.
Marcel Korpel
+1  A: 

//somedomain.com/images/img.jpg is a perfectly valid URI syntax as per RFC 3986: Section 4.2.

It is relative to the current scheme, and therefore it can be very useful when switching between http and https, because you won't need to specify the scheme.

All modern browsers will understand that format, including IE 6.

Daniel Vassallo
+3  A: 

Scheme-relative URL's are particularly useful when you're serving a HTTPS website and you would like to share the same static content like stylesheets, images, scripts and so on as from a HTTP site. Hardcoding http: in the static content links like <link href>, <script src>, <img src> and so on and viewing the webpage itself over https: would cause the average webbrowser to pop a security warning like the following well-known IE security alerts:

alt text

alt text

Serving the "nonsecure" content over a scheme-relative URL would fix this issue.

BalusC