Browser support for URLs beginning with double slash

dtbarne picture dtbarne · Jul 22, 2011 · Viewed 23.8k times · Source

I've recently seen a few links used without a protocol. It didn't seem too difficult to understand - I think it's a great idea and pretty intuitive.

For those of you unaware, using a URL like //example.com/script.js will point to either http://example.com/script.js or https://example.com/script.js depending on whether or not the URL originates from a http or https URL. Including http scripts or images from a https page can be a security concern, for example, so this solves that without the need for protocol detection in your code.

My question is, what sort of browser/OS support is there for it? Is it safe to use in production? It would certainly make things a bit easier.

Simple example and test: http://codetester.org/916c6916

EDIT: Just a follow up that I've been using this for my company's ad server in production for many things without issue for a couple years now.

Answer

oezi picture oezi · Mar 9, 2012

This behavior was part of RFC 1808 (Section 4) which is about 16 years old, so every major browser should (and does) support this.

Sadly, there's a bug with IE7 and -8 that will make them download the resources twice if a protocol-relative URL is used on a link or @import - which shouldn't be a big problem, but is ugly and should be kept in mind.