Html 绝对 URL 省略协议(方案)以保留当前页面之一

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4978235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:41:22  来源:igfitidea点击:

Absolute URLs omitting the protocol (scheme) in order to preserve the one of the current page

htmlhttpurlweb-applicationsprotocol-relative

提问by Bozho

I saw the //somepage.com/resourceurl format. For example:

我看到了//somepage.com/resource网址格式。例如:

<img src="//remotesite.com/image1.jpg" />

The point of this is that if the current page (the page defining the imgtag) is using http, then the request to the remote site is made via http. If it is https - it's https. This eliminates browser warnings of not fully encrypted pages.

这一点是,如果当前页面(定义img标签的页面)正在使用http,那么对远程站点的请求是通过 http 进行的。如果是 https - 它是 https。这消除了未完全加密页面的浏览器警告。

My question is - is this URL format safe to use for all browsers. And is it a standard?

我的问题是 - 这种 URL 格式是否可以安全地用于所有浏览器。它是一个标准吗?

采纳答案by Sarfraz

is this URL format safe to use for all browsers.

这种 URL 格式是否可以安全地用于所有浏览器。

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:

从技术上讲,它根据RFC 3986被称为“网络路径参考” 。这是它的方案:

  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,IE7 和 IE8 会下载文件。

Here is a post written by Paul Irish on the subject:

这是保罗爱尔兰写的一篇关于这个主题的帖子:

回答by xzyfer

Yes I believe it is. Paul Irish coined the term Protocol Relative URL.

是的,我相信是的。保罗爱尔兰创造了术语协议相对 URL

I'd also point out it's part of the HTML5Boilerplateproject which evangelises cross browser compatibility.

我还要指出它是宣传跨浏览器兼容性的HTML5Boilerplate项目的一部分。

Note:there is an edge case in IE6 with google analytics which is mentioned in Paul's article. So it's not perfect.

注意:在 IE6 中有一个边缘情况,谷歌分析在 Paul 的文章中提到。所以它并不完美。

回答by Peter O'Brien

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

指定为在 Google 的 HTML/CSS 样式指南中使用的格式:编辑:最新网址:https://google.github.io/styleguide/htmlcssguide.xml#Protocol