Html url/src/href 属性中的两个正斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9646407/
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-28 23:01:57  来源:igfitidea点击:

Two forward slashes in a url/src/href attribute

htmlhttphttpsrelative-url

提问by Michael Parkin

Possible Duplicate:
URI starting with two slashes … how do they behave?
Absolute URLs omitting the protocol (scheme) in order to preserve the one of the current page
shorthand as // for script and link tags? anyone see / use this before?

可能的重复:
URI 以两个斜杠开头……它们的行为如何?
为了将当前页面速记之一保留为 // 用于脚本和链接标签,绝对 URL 省略了协议(方案)
?有人看过/使用过吗?

I was looking through the source of HTML5 Resetwhen I noticed the following line:

当我注意到以下行时,我正在查看HTML5 Reset的源代码:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

Why does the URL start with two forward slashes? Is this a shorthand for http://?

为什么 URL 以两个正斜杠开头?这是 的简写http://吗?

回答by vzwick

The "two forward slashes" are a common shorthand for "whatever protocol is being used right now".

“两个正斜杠”是“现在正在使用的任何协议”的常见简写。

Best known as "protocol relative URLs", they are particularly useful when elements — such as the JS file in your example — could be loaded from either a httpor a httpscontext. By using protocol relative URLs, you can avoid implementing

最著名的是“协议相对 URL”,当元素(例如示例中的 JS 文件)可以从 ahttphttps上下文加载时,它们特别有用。通过使用协议相对 URL,您可以避免实现

if (window.location.protocol === 'http:') {
    myResourceUrl = 'http://example.com/my-resource.js';
} else {
    myResourceUrl = 'https://example.com/my-resource.js';
}

type of logic all over your codebase (assuming, of course, that the server at example.comis able to serve resources via both httpand https).

整个代码库中的逻辑类型(当然,假设服务器 atexample.com能够通过http和提供资源https)。

A prominent real-world example is the Magento E-Commerce engine: for performance reasons, the shop's pages use plain httpby default, whereas the checkout is httpsenabled.

一个突出的现实世界的例子是 Magento 电子商务引擎:出于性能原因,商店的页面http默认使用普通的,而结账是https启用的。

When hard-coded resources (i.e. promotional banners in the site's header) are referenced by non protocol relative URLs (i.e. http://example.com/banner.jpg), customers reaching the httpsenabled checkout will be greeted with a rather unfriendly

当硬编码资源(即站点标题中的促销横幅)被非协议相对 URL(即http://example.com/banner.jpg)引用时,到达https启用结帐的客户将受到相当不友好的欢迎

"there are insecure elements on this page"

“此页面上存在不安全元素”

prompt - which, as you can imagine, throws the average non-tech-savvy person off.

提示 - 正如您可以想象的那样,这会让普通的非精通技术的人望而却步。

If the aforementioned resource is referenced via //example.com/banner.jpgthough, the browser takes care of prepending the proper protocol.

如果通过//example.com/banner.jpg尽管引用上述资源,浏览器会负责预先准备正确的协议。

tl;dr: With even the slightest possibility of a mixed http/https environment, just use the double slash/protocol relative URLs for loading your resources — assuming that the host serving the content is both http and https enabled.

tl;dr:即使是最轻微的混合 http/https 环境的可能性,也只需使用双斜杠/协议相对 URL 来加载您的资源——假设提供内容的主机同时启用了 http 和 https。

回答by Radu Cugut

It will automatically add https or http, depending on how the request was made.

它会自动添加 https 或 http,具体取决于请求的方式。