使用//而不是protocol://

时间:2011-10-07 22:53:36

标签: html url-scheme

  

可能重复:
  Is it valid to replace with // in a <script src=“…”>?
  Absolute URLs omitting the protocol (scheme) in order to preserve the one of the current page
  Does using //www.example.com in Javascript chose http/https protocol automatically

我正在查看facebook的一些示例代码,我看到了:

<script src="//connect.facebook.net/en_US/all.js"></script>

他们使用//代替http:// - 这是花哨的,我还不知道吗?

3 个答案:

答案 0 :(得分:16)

它被称为“协议相对URL”。类似于以“/”开头的url相对于当前域的根的方式,以“//”开头的URL将链接到指定的主机和路径,但使用当前页面加载的任何协议。

在维基媒体博客上有一个很好的描述,以及它们为什么有用:

http://blog.wikimedia.org/2011/07/19/protocol-relative-urls-enabled-on-test-wikipedia-org/

答案 1 :(得分:2)

基本上,它使您能够吐出一个URL并让它使用当前正在使用的任何协议。

Facebook可能使用相同的HTML代码,无论用户是使用HTTP还是HTTPS。这是一种在不指定协议的情况下完全限定域的方法。

答案 2 :(得分:0)

它是另一种相对URL,它使用与页面相同的协议。