Browser support for URLs beginning with double slash

UrlBrowserCross Browser

Url Problem Overview


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.

Url Solutions


Solution 1 - Url

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.

Solution 2 - Url

If you are developing on a local machine there's possibility it will fail with src="file://host.com/filename".

In this situation you need to specify scheme explicitly: http://host.com/filename or https://host.com/filename.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondtbarneView Question on Stackoverflow
Solution 1 - UrloeziView Answer on Stackoverflow
Solution 2 - UrlMatas VaitkeviciusView Answer on Stackoverflow