Google CDN 上的最新 jQuery 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12608242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Latest jQuery version on Google's CDN
提问by Martin Borthiry
I read in the official docof the Google CDN that this is the src
to jQuery:
我在Google CDN的官方文档中读到这是src
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.
但是,src
每次版本更新时都必须更改我的 jQuery引用,这很烦人。
I've found that if I set the version to 1
then Google returns the latest version of jQuery.
我发现如果我将版本设置为1
那么 Google 会返回最新版本的 jQuery。
http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
/*! jQuery v1.8.2 jquery.com | jquery.org/license */
Is this the right thing to do? Is there any official URL to reference the latest version of jQuery hosted on the Google CDN?
这是正确的做法吗?是否有任何官方 URL 可以引用托管在 Google CDN 上的最新版本的 jQuery?
回答by Blazemonger
UPDATE 7/3/2014:As of now, jquery-latest.js
is no longer being updated.
From the jQuery blog:
2014 年 7 月 3 日更新:截至目前,jquery-latest.js
不再更新。来自jQuery 博客:
We know that http://code.jquery.com/jquery-latest.jsis 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.
We have decided to stop updating this file, as well as the minified copy, keeping both files at version 1.11.1 forever.
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.
我们知道http://code.jquery.com/jquery-latest.js被滥用是因为 CDN 统计数据显示它是最受欢迎的文件。如果开发人员仅使用它来制作本地副本,则情况并非如此。
我们已决定停止更新此文件以及缩小的副本,将这两个文件永远保留在 1.11.1 版中。
Google CDN 团队已加入我们的工作,以防止无意的网络损坏,并且不再更新http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js 上的文件 。该文件也将锁定在 1.11.1 版本。
The following, now moot, answer is preserved here for historical reasons.
以下,现在没有实际意义,由于历史原因,这里保留了答案。
Don't do this. Seriously, don't.
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 的主要版本确实有效,但这是一个坏主意——每次十进制更新都会添加和弃用全新的功能。如果您在没有完全测试代码的情况下自动更新 jQuery ,那么当某些关键方法的 API 发生变化时,您可能会面临意外的惊喜。
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 版本。这样做甚至可能会给您的代码添加以前不存在的新错误。没有负责任的开发人员会在没有彻底测试的情况下自动包含来自另一个站点的新代码。
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?
没有充分的理由总是使用最新版本的 jQuery。旧版本在 CDN 上仍然可用,如果它们适合您的目的,那么为什么要更换它们呢?
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.
第二个但可能更重要的问题是缓存。许多人在 CDN 上链接到 jQuery,因为许多其他站点都这样做,并且您的用户很有可能已经缓存了该版本。
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.)
问题是,缓存仅在您提供完整版本号时才有效。如果您提供部分版本号,则不会发生远期缓存——因为如果这样做,一些用户将从同一 URL 获得不同的 jQuery 次要版本。(假设指向 1.7 的链接有一天指向 1.7.1,第二天指向 1.7.2。浏览器如何确保它今天获得最新版本?答案:没有缓存。)
In fact here's a breakdownof several options and their expiration settings...
http://code.jquery.com/jquery-latest.min.js(no cache)
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 hour)
http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js(1 year)
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年)
So, by linking to jQuery this way, you're actually eliminatingone of the major reasons to use a CDN in the first place.
因此,通过以这种方式链接到 jQuery,您实际上首先消除了使用 CDN 的主要原因之一。
http://code.jquery.com/jquery-latest.min.jsmay 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.
http://code.jquery.com/jquery-latest.min.js也可能不会总是为您提供您期望的版本。在撰写本文时,它链接到最新版本的 jQuery 1.x,即使 jQuery 2.x 也已发布。这是因为 jQuery 1.x 与包括 IE 6/7/8 在内的旧浏览器兼容,而jQuery 2.x 不兼容。如果您想要最新版本的 jQuery 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.
两个版本具有相同的API,因此兼容浏览器没有感知差异。但是,jQuery 1.x 的下载量比 2.x 大。
回答by jrummell
I don't know if/where it's published, but you can get the latest release by omitting the minor and build numbers.
我不知道它是否/在哪里发布,但是您可以通过省略次要和内部版本号来获得最新版本。
Latest 1.8.x:
最新的 1.8.x:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
Latest 1.x:
最新 1.x:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
However, do keep in mind that these links have a much shorter cache timeout than with the full version number, so your users may be downloading them more than you'd like. See The crucial .0 in Google CDN references to jQuery 1.x.0for more information.
但是,请记住,这些链接的缓存超时时间比完整版本号要短得多,因此您的用户下载它们的次数可能比您希望的要多。有关详细信息,请参阅Google CDN 中的关键 .0 对 jQuery 1.x.0 的引用。
回答by Abdul Munim
If you wish to use jQuery CDN other than Google hosted jQuery library, you might consider using this and ensuresuses the latest version of jQuery:
如果您希望使用 Google 托管的 jQuery 库以外的 jQuery CDN,您可以考虑使用它并确保使用最新版本的 jQuery:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
回答by Bob Stein
To use the latest jquery version hosted by Google
使用 Google 托管的最新 jquery 版本
Humans:
人类:
Get the snippet:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
- Put it in your code.
- Make sure it works.
- 把它放在你的代码中。
- 确保它有效。
Bots:
机器人:
- Wait for a human to do it.
- 等人来做。
回答by user0910
There are updated now and then, just keep checking for the latest version.
不时更新,请继续检查最新版本。