我看到了 //somepage.com/resource的网址格式,例如:
//somepage.com/resource
<img src="//remotesite.com/image1.jpg" />
关键在于,如果当前页面(定义 img标记的页面)正在使用 http,那么对远程站点的请求将通过 http 发出。如果是 https-就是 https。这将消除未完全加密的页面的浏览器警告。
img
http
我的问题是——这种 URL 格式对所有浏览器都安全吗? 它是一种标准吗?
Yes I believe it is. Paul Irish coined the term Protocol Relative URL.
I'd also point out it's part of the HTML5Boilerplate project which evangelises cross browser compatibility.
Note: there is an edge case in IE6 with google analytics which is mentioned in Paul's article. So it's not perfect.
is this URL format safe to use for all browsers.
I can't say anything for sure, but you should be able to test it in different browsers.
And is it a standard?
Technically, it is called "network-path reference" according to RFC 3986. Here is the scheme for it:
relative-ref = relative-part [ "?" query ] [ "#" fragment ] relative-part = "//" authority path-abempty / path-absolute / path-noscheme / path-empty
There is a problem though, when used on a <link> or @import, IE7 and IE8 download the file.
<link>
@import
Here is a post written by Paul Irish on the subject:
Should be safe.
Is specified as format to use in Google's HTML/CSS styleguide: EDIT: latest url : https://google.github.io/styleguide/htmlcssguide.xml#Protocol