以“//”开始外部站点链接

发布于 2025-01-04 16:37:10 字数 817 浏览 0 评论 0原文

可能的重复:
替换为 // 是否有效在

我现在在一些网站上看到过这种情况。一个简单的例子是查看维基百科登陆页面的源代码:

<link rel="shortcut icon" href="//en.wikipedia.org/favicon.ico" />
<link rel="apple-touch-icon" href="//upload.wikimedia.org/wikipedia/commons/f/f1/Wikipedia-mobile-icon.png" />
<link rel="copyright" href="//creativecommons.org/licenses/by-sa/3.0/" />

是否存在以 // 开头外部链接的标准?这样做是否只是为了避免下载额外的 https?: 并保存一些字符,因为大多数浏览器都足够智能,可以自行添加方案的其余部分?或者这是一件合法的事情,也许还有其他原因?

Possible Duplicate:
Is it valid to replace with // in a <script src=“…”>?
Links start with two slashes

I've seen this on a few sites now. An easy example is to look at the Wikipedia landing page's source:

<link rel="shortcut icon" href="//en.wikipedia.org/favicon.ico" />
<link rel="apple-touch-icon" href="//upload.wikimedia.org/wikipedia/commons/f/f1/Wikipedia-mobile-icon.png" />
<link rel="copyright" href="//creativecommons.org/licenses/by-sa/3.0/" />

Is there some standard with starting external links with //? Is this just done to avoid having to download the extra https?: and save some characters because most browsers are smart enough to add the rest of the scheme on there own? Or is this a legitimate thing, maybe with some other reason?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

或十年 2025-01-11 16:37:10

这通常称为协议相关 URL,允许浏览器使用与加载页面相同的协议来下载资源。因此,如果用户通过 https url 加载页面,则 // 指定的资源将通过 https 加载,否则将加载通过常规的http

它可以帮助您防止的一件事是旧版本 IE 中出现丑陋的用户不友好消息,指出该页面包含安全和非安全资源。

Paul Irish 就此写了一篇很好的博文:

That is usually called protocol-relative URLs and allows for the browser to download the resource using the same protocol as the page was being loaded with. So if the user had loaded the page through a https url, the resources specified with // will be loaded through https, otherwise they are loaded through regular http.

One thing it can help you prevent is the ugly user-unfriendly message in older versions of IE, stating that the page contains both secure and non-secure resources.

Paul Irish has written a good blog post about this:

陪你搞怪i 2025-01-11 16:37:10

这样您就可以在不指定http或https的情况下下载资源;它将使用您当前正在使用的任何内容。

This allows you to download the resources without specifying http or https; it will use whatever you're currently using.

深空失忆 2025-01-11 16:37:10

不在 URL 中指定方案将导致浏览器采用当前方案。您可以在这里阅读一篇关于此的好文章:

http://paulirish.com/2010/ the-protocol-relative-url/

这是它的主要好处:

如果浏览器通过 HTTPS 查看当前页面,则
它将使用 HTTPS 协议请求该资产,否则它将
通常*使用 HTTP 请求。这可以防止可怕的“此页面
IE 中包含安全和非安全项目”错误消息,
将您的所有资产请求保持在同一协议内。

有一些怪癖...

当用于样式表的 @import 时,IE7 和 IE8
下载该文件两次。然而,所有其他用途都很好。

...但一般来说您可以安全地使用它。

Not specifying a scheme in the URL will cause the browser to assume the current scheme. You can read a good article about this here:

http://paulirish.com/2010/the-protocol-relative-url/

This is the main benefit of it:

If the browser is viewing that current page in through HTTPS, then
it'll request that asset with the HTTPS protocol, otherwise it'll
typically* request it with HTTP. This prevents that awful "This Page
Contains Both Secure and Non-Secure Items" error message in IE,
keeping all your asset requests within the same protocol.

There are some quirks...

When used on a <link> or @import for a stylesheet, IE7 and IE8
download the file twice. All other uses, however, are just fine.

... but in general you can use this safely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文