我在Google CDN的official doc中读到,这是src to jQuery:

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

However, it is annoying to have to change my jQuery src reference at each version update.

我发现,如果我将版本设置为1,那么Google将返回最新版本的jQuery.

http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
/*! jQuery v1.8.2 jquery.com | jquery.org/license */

这样做对吗?是否有任何官方URL来引用谷歌CDN上托管的最新版本的jQuery?

推荐答案

UPDATE 7/3/2014: As of now, jquery-latest.js is no longer being updated. From the jQuery blog:

We know that http://code.jquery.com/jquery-latest.js is abused because of the CDN statistics showing it’s the most popular file. That wouldn’t be the case if it was only being used by developers to make a local copy.

我们决定停止

The Google CDN team has joined us in this effort to prevent inadvertent web breakage and no longer updates the file at http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js. That file will stay locked at version 1.11.1 as well.

由于历史原因,下面这个答案现在还没有定论.


Don't do this. Seriously, don't.

链接到jQuery的主要版本确实有效,但这不是一个好主意——每次十进制更新都会添加和弃用全新的功能.如果您在不测试代码COMPLETELY的情况下自动更新jQuery,那么如果某个关键方法的API发生了更改,您将面临意外的风险.

下面是您应该做的事情:使用最新版本的jQuery编写代码.测试它,调试它,当它准备好投入生产时发布它.

Then, when a new version of jQuery rolls out, ask yourself: Do I need this new version in my code? For instance, is there some critical browser compatibility that didn't exist before, or will it speed up my code in most browsers?

If the answer is "no", don't bother updating your code to the latest jQuery version. Doing so might even add NEW errors to your code which didn't exist before. No responsible developer would automatically include new code from another site without testing it thoroughly.

There's simply no good reason to ALWAYS be using the latest version of jQuery. The old versions are still available on the CDNs, and if they work for your purposes, then why bother replacing them?


第二个但可能更重要的问题是缓存.许多人在CDN上链接到jQuery,因为许多其他网站都链接到了,而您的用户很有可能已经缓存了该版本.

问题是,caching only works if you provide a full version number.如果您提供了部分版本号,那么future 的缓存就不会发生——因为如果提供了部分版本号,一些用户将从同一个URL获得不同的jQuery次要版本.(假设指向1.7的链接一天指向1.7.1,第二天指向1.7.2.浏览器如何确保今天获得最新版本?回答:无缓存.)

事实上,有here's a breakdown个 Select 权及其到期日

http://code.jquery.com/jquery-latest.min.js(无缓存)

http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js (1 hour)

http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js (1

http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js (1 year)

So, by linking to jQuery this way, you're actually eliminating one of the major reasons to use a CDN in the first place.


http://code.jquery.com/jquery-latest.min.js可能也不一定能提供您期望的版本.在 compose 本文时,它链接到jQuery 1.x的最新版本,尽管jQuery 2.x也已经发布.这是因为jQuery 1.x与较旧的浏览器兼容,包括IE6/7/8和jQuery 2.x is not.如果您需要最新版本的jQuery 2.x,那么(目前)您需要显式指定.

这两个版本具有相同的API,因此兼容的浏览器没有感知上的差异.但是,jQuery 1.x的下载量比2.x大.

Jquery相关问答推荐

为什么在 jQuery 插件中返回 this.each(function())?

将日期时间从 javascript 传递给 c# (Controller)

为什么 .join() 不能与函数参数一起使用?

在 Bootstrap 3 中向工具提示添加换行符

jQuery javascript 正则表达式 将
替换为 \n

在 jQuery 中,我想删除 div 中的所有 HTML

jQuery中的正则表达式字段验证

.trigger() 与 .click() 中的 jQuery 优势/差异

jQuery ajax() 使用成功、错误和完成与 .done()、.fail() 和 always()

你如何获得文本区域中的光标位置?

Jquery:如何判断元素是否具有某些 css 类/样式

jQuery - 确定输入元素是文本框还是 Select 列表

jQuery确定匹配的类是否具有给定的ID

删除除一个之外的所有类

表单 propType 失败:您在没有 `onChange` 处理程序的情况下向表单字段提供了 `value` props

添加到数组 jQuery

textarea 的 val() 与 text()

JQuery 判断 DOM 中的重复 id

使用 Ajax 下载并打开 PDF 文件

使用 Chosen 插件更改 Select 中的 Select