Latest jQuery version on Google's CDN
Asked Answered
T

4

103

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>

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

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 */

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

Tuberculin answered 26/9, 2012 at 18:36 Comment(0)
T
183

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.

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.

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.

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.

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?


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.

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

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

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.

Thorsten answered 26/9, 2012 at 18:39 Comment(5)
Thanks you for your point of view, but in my case it is better to use the latest version. however, I'm partially agree with your opinion, but some new version also fix old bugs. Also, latest version has more probability to be in cache, that is one of the cdn benefits.Tuberculin
a good example of this happening is developing in any jquery < 1.4 then upgrading past 1.4. There were changes in accessing properties and how ajax requests handled JSON data in the callback.Buckinghamshire
just to clarify.. I was asking for a single page that use trivial part of jquery... I understand your good point, but I just want to know if google is offering an url to get latest version.Tuberculin
@Buckinghamshire - jQuery 1.9 introduced a lot of breaking changes as well. I have run into a lot of bugs over the years due to backwards compatibility issues in jQuery. And I absolutely agree with Blazemonger's advice. Under no circumstances should you simply point to the "latest jQuery" URL. You'd be at the complete mercy of the jQuery team, and a new jQuery release could easily break your site. Not only could it happen, but it will likely happen at some point.Do
nice to learn that caching is based on the version detail level!Nikolia
K
8

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:

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

Latest 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.0 for more information.

Kane answered 26/9, 2012 at 18:38 Comment(3)
You can alternatively get the latest release from code.jquery.com/jquery.min.js it usually gets updated a few days before the other cdnsDoroteya
While you can pull up the latest 1.8.x that way, you can't pull up the latest 1.9 or any minor version number after with similar links. In addition, the "latest 1.x" link currently points to 1.11.1 when the latest is actually 1.11.3.Thorsten
@Thorsten That's correct. This changed after I answered this. You have a great explanation in your update!Kane
P
8

If you wish to use jQuery CDN other than Google hosted jQuery library, you might consider using this and ensures uses the latest version of jQuery:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
Paleethnology answered 26/9, 2012 at 18:42 Comment(3)
This pulled up 1.9.1 instead of 2.0Facelifting
v2.x is not compatible with v1.x. Any site that is pointing to this url will actually screw up the sites if it was updated to v2Paleethnology
That link no longer pulls up the latest version of 1.x, eitherThorsten
M
3

To use the latest jquery version hosted by Google

Humans:

  1. https://developers.google.com/speed/libraries/#jquery

  2. Get the snippet:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  1. Put it in your code.
  2. Make sure it works.

Bots:

  1. Wait for a human to do it.
Masterpiece answered 20/11, 2017 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.