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

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

问题描述

我有以下元素:

<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?

推荐答案

根据 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="http://..."&gt; 中用//替换 http://是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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