开发者

Browser support for URLs beginning with double slash

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 id开发者_如何转开发ea 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜