用< script src =" http://..."">替换http://是否有效? [英] Is it valid to replace http:// with // in a <script src="http://...">?

查看:134
本文介绍了用< script src =" http://..."">替换http://是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下元素:

I have the following element:

<script type="text/javascript" src="https://cdn.example.com/js_file.js"></script>

在这种情况下,该网站是HTTPS,但该网站也可能只是HTTP。 (JS文件位于另一个域中。)为了方便起见,我想知道是否可以执行以下操作:

In this case the site is HTTPS, but the site may also be just HTTP. (The JS file is on another domain.) I'm wondering if it's valid to do the following for convenience sake:

<script type="text/javascript" src="//cdn.example.com/js_file.js"></script>

我想知道是否可以移除 http: https:

I'm wondering if it's valid to remove the http: or https:?

这似乎可以在我测试过的任何地方工作,但是有没有这种情况

It seems to work everywhere I have tested, but are there any cases where it doesn't work?

推荐答案

没有方案的相对URL(http:或https :)是有效的, RFC 3986:统一资源标识符(URI):通用语法,第4.2节。如果一个客户端扼杀了它,那么这是客户端的错,因为它们不符合RFC中指定的URI语法。

A relative URL without a scheme (http: or https:) is valid, per RFC 3986: "Uniform Resource Identifier (URI): Generic Syntax", Section 4.2. If a client chokes on it, then it's the client's fault because they're not complying with the URI syntax specified in the RFC.

您的示例是有效的,应该可以工作。我在交通繁忙的网站上使用了这种相对URL方法,并且没有任何投诉。此外,我们测试我们的网站在Firefox,Safari,IE6,IE7和Opera。这些浏览器都了解URL格式。

Your example is valid and should work. I've used that relative URL method myself on heavily trafficked sites and have had zero complaints. Also, we test our sites in Firefox, Safari, IE6, IE7 and Opera. These browsers all understand that URL format.

这篇关于用&lt; script src =&quot; http://...&quot;&quot;&gt;替换http://是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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