What's the best way to load jQuery? [duplicate]
Asked Answered
R

4

16

Possible Duplicate:
Where do you include the jQuery library from? Google JSAPI? CDN?

I have an application that uses jQuery. The application could use almost any recent version of jQuery such as 1.7 or 1.8.

Can anyone give me a suggestion as to how I could code my application to maximize the chances of it finding a recent version of jQuery that would meet my needs in the users cache. For example should I try and look for a version from the google CDN and in which case which version should I try and look for?

Radiography answered 8/9, 2012 at 16:56 Comment(1)
Should not be marked as duplicate. The "duplicate" is crowded and contains outdated information. This is more up-to-dateTatter
M
23

The use of a Content Delivery Network (CDN) for public web sites is quite common. To reference one you include them with a script tag like any other local file:

jQuery example

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

Google example

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

Microsoft example

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.min.js" type="text/javascript"></script>

Should I always reference the latest version?

There is a risk when doing so, when for example using the jQuery CDN to always point to the latest version.

In the last few versions several methods have been deprecated, i.e: toggle() - for mouse events, live(), die() and others.

As far as I know from the jQuery forums is that those deprecated methods will be permanently removed in version 1.9.

In addition jQuery plans to release 1.9 and 2.0 close together. However 2.0 is not a continuation of 1.9 but instead will be developed on along-side.

2.0 will not support IE6, IE7 or IE8. jQuery 1.9 will stay compatible with all previous browser versions.

Read about it here.

For those reasons I would not recommend to automatically always point to the latest version but explicitly reference the specific version you are supporting.

What if the CDN is down

It rarely happens but it could happen that the CDN is down. Just in case, so you don't have to suffer the consequence, you can implement a fallback plan.

// Check if jQuery was initialized and if not (CDN was down for example), then
// load jQuery from a local source.
if(typeof jQuery === 'undefined'){
    document.write(unescape("%3Cscript src='yourlocalpath/jquery.1.x.min.js' type='text/javascript'%3E%3C/script%3E"));
}

CDN availability and performance evaluation

Regarding the quality and performance on available CDNs I came across this very interesting article on royal.pingdom.com.

The report from Pingdom revealed that the most commonly used, and free hosts of jQuery; Google, Microsoft and Media Temple; have proved reliable but with inconsistent performance.

To evaluate the networks, Pingdom performed tests from multiple locations across Europe and North America, once per minute, around the clock for 30 days.

The results found that all three offered excellent availability but that wasn't the case for performance.

For sites that don't use HTTPS or secure servers, Media Temple was by far the fastest followed by Google in Europe but lagged behind in North America. For HTTPS sites Google was the fastest in Europe with Google and Microsoft performing similar in North America.

Microsoft performed the worst in Europe but was even with Google in North America overall.

Maltose answered 8/9, 2012 at 17:11 Comment(1)
Is it a typo or is it intentional that the Google version is missing the type="text/javascript"?Drafty
S
7

Pick the most recent version of jQuery that meets your needs. At this moment, that would be 1.8. Then link to it on a free and widely used CDN (Google or Microsoft probably).

You should link to a specific version of jQuery. Though there is a link for the latest version of jQuery, you should NOT link to that because when that changes, your site could break without you knowing it. You should link only to a specific version of jQuery. Here's the code for linking to 1.8.1 on the Google CDN.

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

If you really want to optimize your browser cache hits, you would pick and test with a version of jQuery that is widely deployed now (I believe there are web statistics somewhere on how much each version is being used) and then you would regularly update that to track the most used version. I suspect that this is micro-optimizing and just picking any recent version that you've tested with is suitable and will give you the benefits of using a common CDN.

Serenaserenade answered 8/9, 2012 at 17:0 Comment(0)
E
4

I would recommend referencing a static version number from the Google or Microsoft CDN. Use the latest version for development now. If you make changes that require newer features, just change your reference in your layouts.

The Google CDN looks like:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
Erhard answered 8/9, 2012 at 17:2 Comment(0)
D
2

For the latest version, you can load directly from jQuery:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

There are also other Content Delivery Networks that you can load from as well.

Some of the popular CDNs are:

  • jQuery (code.jquery.com)
  • Microsoft (ajax.microsoft.com)
  • Google (ajax.googleapis.com)
Davidoff answered 8/9, 2012 at 16:57 Comment(3)
I would like to use a CDN but which one would be the best to choose and how should I code it to look for the jQuery that is most likely to be in the cache?Radiography
At the point where you are deciding between Google, jQuery, or Microsoft it really doesn't matter. All of them have similar reliability and performance.Davidoff
It is dangerous to link to jquery-latest.min.js because jQuery could make changes in a future version that could break your site (it happens all the time) and your site would automatically inherit that and get broken.Serenaserenade

© 2022 - 2024 — McMap. All rights reserved.