Google CDN上的最新jQuery版本 [英] Latest jQuery version on Google's CDN

查看:157
本文介绍了Google CDN上的最新jQuery版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google CDN的官方文档中了解到,这是src到jQuery:

I read in the official doc of the Google CDN that this is the src to jQuery:

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

但是,每次更新版本时都必须更改我的jQuery src引用很烦人.

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

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

I've found that if I set the version to 1 then Google returns the latest version of jQuery.

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

这是正确的做法吗?是否有任何官方URL可以引用Google CDN上托管的jQuery的最新版本?

Is this the right thing to do? Is there any official URL to reference the latest version of jQuery hosted on the Google CDN?

推荐答案

更新7/3/2014:到目前为止,jquery-latest.js不再被更新. 来自 jQuery博客:

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

我们知道 http://code.jquery.com/jquery-latest.js 被滥用 由于CDN统计信息 显示它是最受欢迎的文件.如果不是这样的话 只是被开发人员用来制作本地副本.

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.

我们决定停止 更新此文件以及缩小的副本,同时保留两个文件 永远是1.11.1版.

We have decided to stop updating this file, as well as the minified copy, keeping both files at version 1.11.1 forever.

Google CDN团队已加入我们的行列,以防止 意外的网页损坏,并且不再在以下位置更新文件 http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js .该文件 也将锁定在1.11.1版本.

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.

以下出于历史原因保留了以下现成的答案.

The following, now moot, answer is preserved here for historical reasons.

请勿执行此操作.认真的,不要.

链接到jQuery的主要版本确实可行,但这是一个坏主意-每次小数更新都会添加和弃用整个新功能.如果您在不测试代码完全的情况下自动更新jQuery,则如果某些关键方法的API发生了更改,您将面临意外的意外风险.

Linking to major versions of jQuery does work, but it's a bad idea -- whole new features get added and deprecated with each decimal update. If you update jQuery automatically without testing your code COMPLETELY, you risk an unexpected surprise if the API for some critical method has changed.

这是您应该做的:使用最新版本的jQuery编写代码.测试,调试,准备生产时将其发布.

Here's what you should be doing: write your code using the latest version of jQuery. Test it, debug it, publish it when it's ready for production.

然后,当新版本的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?

如果答案为否",请不要费心将代码更新为最新的jQuery版本. 这样做甚至可能向您的代码中添加以前不存在的新错误.没有负责任的开发人员会在未经彻底测试的情况下自动从其他站点包含新代码.

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.

没有充分的理由总是使用最新版本的jQuery. CDN仍然可以使用旧版本,如果它们可以满足您的需求,那么为什么还要替换它们呢?

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,因为有许多其他站点都这样做了,并且您的用户很有可能已经缓存了该版本.

A secondary, but possibly more important, issue is caching. Many people link to jQuery on a CDN because many other sites do, and your users have a good chance of having that version already cached.

问题是,只有在您提供完整版本号的情况下,缓存才有效.如果提供部分版本号,则不会进行远距离缓存-因为如果这样做,则某些用户将从同一URL获得不同的jQuery次要版本. (假设指向1.7的链接一天指向1.7.1,第二天指向1.7.2.浏览器将如何确保它今天获得了最新版本?答案:没有缓存.)

The problem is, caching only works if you provide a full version number. If you provide a partial version number, far-future caching doesn't happen -- because if it did, some users would get different minor versions of jQuery from the same URL. (Say that the link to 1.7 points to 1.7.1 one day and 1.7.2 the next day. How will the browser make sure it's getting the latest version today? Answer: no caching.)

实际上这是几个选项的分类及其有效期 设置...

In fact here's a breakdown of several options and their expiration settings...

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

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

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 年)

因此,通过这种方式链接到jQuery,实际上是消除首先使用CDN的主要原因之一.

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 可能不会总是给您您期望的版本.在撰写本文时,即使jQuery 2.x也已经发布,它也链接到最新版本的jQuery1.x.这是因为jQuery 1.x与包括IE 6/7/8和

http://code.jquery.com/jquery-latest.min.js may not always give you the version you expect, either. As of this writing, it links to the latest version of jQuery 1.x, even though jQuery 2.x has been released as well. This is because jQuery 1.x is compatible with older browsers including IE 6/7/8, and jQuery 2.x is not. If you want the latest version of jQuery 2.x, then (for now) you need to specify that explicitly.

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

The two versions have the same API, so there is no perceptual difference for compatible browsers. However, jQuery 1.x is a larger download than 2.x.

这篇关于Google CDN上的最新jQuery版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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