Is there any downside for using a leading double slash to inherit the protocol in a URL? i.e. src="//domain.com"

Rob Volk picture Rob Volk · Jan 11, 2011 · Viewed 47.8k times · Source

I have a stylesheet that loads images from an external domain and I need it to load from https:// from secure order pages and http:// from other pages, based on the current URL. I found that starting the URL with a double slash inherits the current protocol. Do all browsers support this technique?

html ex:

<img src="//cdn.domain.com/logo.png" />

css ex:

.class { background: url(//cdn.domain.com/logo.png); }

Answer

Remy Lebeau picture Remy Lebeau · Jan 11, 2011

If the browser supports RFC 1808 Section 4, RFC 2396 Section 5.2, or RFC 3986 Section 5.2, then it will indeed use the page URL's scheme for references that begin with "//".