views:

45

answers:

2

Scheme relative URLs are something that I've just found out about - where you don't specify the scheme of a URL and it picks it up from the current context.

For example: <img src="//domain.com/img.png" /> will resolve to https://domain.com/img.png if the current scheme is HTTPS or http://domain.com/img.png if it is not.

This seems like a very easy way to resolve those pesky problems of calling an external script or image on an SSL page without bringing up the dreaded error that some content on a page is not secure.

The benefit seems obvious, but what I don't seem to be able to find is a huge amount of information on this and was wondering if anyone had any experience or references about scheme relative URLs (good or bad)?

Whilst I'm trying to discover if there are any browsers that this causes issues with (I've been successful with IE6-8, Chrome and Firefox), I'm also interested to find out if anyone has any experience using this in different languages. For example, would it work if you were to issue a Response.Redirect with a scheme relative URL in ASP?

+4  A: 

//domain.com/img.png is a perfectly valid URI syntax as per RFC 3986: Section 4.2.

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

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

Further reading on Stack Overflow:

Daniel Vassallo
Such URLs are not new. They are part of the URL specification since 1995 (see [RFC 1808](http://tools.ietf.org/html/rfc1808)).
Gumbo
I would not necessarily trust any kind of client to parse these URLs correctly, but the major browsers definitely will.
Pekka
Is there any reason why Google would choose to do it the other way then? - http://code.google.com/apis/analytics/docs/tracking/gaTrackingOverview.html
Dan Atkinson
@Dan Atkinson: For Google Analytics it is probably much more important to be compatible with any obscure user agent out there, and probably they found that this is more reliable. However, I guess that in general, our web applications don't have to be compatible with browsers from the Netscape 2 era.
Daniel Vassallo
That's as good a reason as any! Thanks!
Dan Atkinson
Thanks for the links and the RFC. I guess I couldn't find it because I'd looked at the older RFCs. I'd still be interested how the redirects would handle it, but I can investigate that a little bit myself later on. Thanks! :)
Jonathon
A: 

I'm always using absolute URL's (generated) in case someone is using a broken proxy.

Reasons why I might drop absolute URL's and use relative one's:

  • Smaller HTML, as it does not have to include http://example.com/blablabla...
  • Do not have to make a variable (constant) available across the file containing the http://e/b...
  • Better readable code, if I need to review code, I don't have to skip through constant URL's.
Lekensteyn