在 Javascript 中使用//www.example.com 是否会自动选择 http/https 协议 [英] Does using //www.example.com in Javascript chose http/https protocol automatically

查看:33
本文介绍了在 Javascript 中使用//www.example.com 是否会自动选择 http/https 协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

省略 http 似乎使它使用 HTTP 连接,但我没有启用 HTTPS 的站点来测试它是否也适用于 HTTPS.

Leaving out the http seems to make it connect using HTTP, but I don't have a site with HTTPS enabled to test whether its true for HTTPS also.

使用//www.example.com/test.js 是否会根据主文档的协议自动选择协议?

Does using //www.example.com/test.js automatically pick the protocol based on the main document's protocol?

推荐答案

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

有一篇关于这个问题的很棒的文章.包括在此处:

has a great article regarding this very issue. Included here:

2010 年 10 月 27 日

October 27th, 2010

作者:保罗爱尔兰

这个小技巧可以让你摆脱困境:

There's this little trick you can get away with that'll save you some headaches:

<img src="//domain.com/img/logo.png">

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

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.

*当然,如果您在本地查看文件,它会尝试使用 file://协议请求文件.

*Of course, if you're viewing the file locally, it'll try to request the file with the file:// protocol.

我们在 HTML5 Boilerplate 中使用这个技巧从 Google CDN 中巧妙地请求 jQuery:

We use this trick in the HTML5 Boilerplate for a clever request of jQuery off the Google CDN:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.4.2.js"%3E%3C/script%3E'))</script>

从技术上讲,这称为网络路径参考".根据 RFC 3986.哦,如果您想真正正确,您将使用术语方案";而不是协议"在谈论网址时.

Technically, this is called a "network-path reference" according to RFC 3986. Oh and if you want to be truly correct, you'll use the term "scheme" instead of "protocol" when talking about URLs.

这个技巧在 CSS 中也很好用:

This trick also works fine in CSS:

.omgomg { background: url(//websbestgifs.net/kittyonadolphin.gif); }

……假设您指向的站点在 HTTP 和 HTTPS 上都可以使用此资产.

… assuming the site you're pointing to has this asset available on both HTTP and HTTPS.

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

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

感谢 miketaylr、ralphholzmann、annevk 和 ajaxian,我想我是 4 年前在那里学到的?也许?

Thx to miketaylr, ralphholzmann, annevk for smarts on this, and ajaxian, where I think I learned it like 4 years ago? maybe?

2011.01.23:但是……在谷歌分析片段上使用这个怎么样?是的,当然,那不是很好.所以我与 Google Analytics javascript 首席开发人员(上帝,我喜欢在 google 工作)一起工作,看看我们是否可以做到这一点......结果我们不能.在 IE6 中有一个边缘错误会导致对话框爆炸……在某些安全设置下(不确定它们是否为默认设置),当请求非ssl"子域时.截图在这里.因此,如果您不关心 IE6,请随意从您的 GA 代码段中删除 40 个字节.否则您将需要那个三元运算符.:)

2011.01.23: But.. what about using this on the google analytics snippet? Yes of course, wouldn't that be nice.. So I worked with the Google Analytics javascript lead developer (God, I love working at google) to see if we could do this.. turns out we can't. There is an edgecase bug in IE6 that causes a dialog to blow up… under some security settings (unsure if they are default) when requesting form the non-'ssl' subdomain. screenshot here. So feel free to take 40 bytes off your GA snippet if you don't care about IE6.. otherwise you're gonna need that ternary operator. :)

这篇关于在 Javascript 中使用//www.example.com 是否会自动选择 http/https 协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆