浏览器支持以双斜杠开头的 URL
我最近看到一些没有协议的链接。它看起来并不太难理解——我认为这是一个好主意并且非常直观。
对于那些不知道的人来说,使用像 //example.com/script.js
这样的 URL 将指向 http://example.com/script.js
或 < code>https://example.com/script.js 取决于 URL 是否源自 http 或 https URL。例如,包含来自 https 页面的 http 脚本或图像可能会引起安全问题,因此无需在代码中进行协议检测即可解决该问题。
我的问题是,有什么类型的浏览器/操作系统支持?在生产中使用安全吗?这肯定会让事情变得容易一些。
简单的例子和测试: http://codetester.org/916c6916
编辑: 只是后续,几年来我一直在我公司的广告服务器生产中使用它来完成许多事情,没有出现任何问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此行为是 RFC 1808(第 4 节)的一部分,该文件是关于16 岁了,所以每个主要浏览器都应该(并且确实)支持这一点。
遗憾的是,IE7 和 -8 存在一个错误,这将使它们下载如果在
link
或@import
上使用协议相对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.如果您在本地计算机上进行开发,则可能会失败并显示
src="file://host.com/filename"。
在这种情况下,您需要显式指定方案:
http: //host.com/filename
或https://host.com/filename
。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
orhttps://host.com/filename
.