以//开头的 URL 是否有效? [英] Is a URL starting with // valid?

查看:38
本文介绍了以//开头的 URL 是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以看到很多 HTML 页面使用 src="//example.com/myjavascript.js" 来包含一个 .js 并让浏览器使用 http:///https:// 取决于页面 URL 的当前方案.

We can see many HTML page using src="//example.com/myjavascript.js" to include a .js and let the browser use http:// / https:// depending on the current scheme of the page URL.

这是一个有效的 URI 吗?

Is this a valid URI ?

推荐答案

是的,它绝对有效.它是方案相关"或协议相关"URI.从一开始就在规范中.对处理 http/https 问题很有帮助.

Yes, it is definitely valid. It is a "scheme relative" or "protocol relative" URI. In the spec since the beginning. Quite helpful to deal with http/https issues.

阅读更好的描述和警告:替换http是否有效://with//在 <script src="http://...">?

Read a much better description and caveats: Is it valid to replace http:// with // in a <script src="http://...">?

需要牢记的几点:

  • IE 7 & 有一些小问题8 次下载
  • 如果您正在查看文件中的 HTML 页面,浏览器将使用 file:// 替换该方案,而不是像使用完整的 URL 开头那样从服务器加载您的 JS 文件http://https://.
  • There are some minor issues on IE 7 & 8 with double downloads
  • If you are viewing an HTML page from a file the browser will replace the scheme with file://, and not load your JS file from the server like it would with a full URL starting with http:// or https://.

针对现代网络开发实践进行

虽然 URL 仍然有效,但现在建议将 https 用于第三方资源(并从安全页面提供这些资源).多年前的性能或兼容性问题在很大程度上已通过更新的协议和浏览器得到解决.

While the URL is still valid, it is now recommended to use https for third party resources (and, serve those resources from secure pages). The performance or compatibility issues from years ago are largely resolved with updated protocols and browsers.

这篇关于以//开头的 URL 是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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