Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to include latest version of jQuery?

I'm developing sites using Wordpress and I want to use the lates version of jQuery.

To make sure I use the lates version, I have found this piece of code from Binary Bonsai's example.

What I see, is that he actually links to jQuery at Google API.

So my question is, what is better.

To link to jQuery on an external page?
http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

or to have the jquery file locally?
wp_enqueue_script('jquery', '/js/jquery-1.4.2.min.js');

UPDATE

Thanks to Colin, the answer is actually here:
Why should I use Google's CDN for jQuery?

But tjust in case you don't have Google in your search term, like me, I'll copy the answer from John Gietzen

  1. It increases the parallelism available.
    (Most browsers will only download 3 or 4 files at a time from any given site)

  2. It increases the chance that there will be a cache-hit.
    (As more sites follow this practice, more users have the file already ready.)

  3. It ensures that the payload will be as small as possible.
    (Google can pre-gzip-compress the file, making the time-to-download very small.)

  4. It reduces the amount of bandwidth used by the server.
    (Google is basically offering free bandwidth.)

  5. It ensures that the user will get a geographically close response.
    (Google has servers all over the world, further decreasing the latency.)

like image 826
Steven Avatar asked Dec 02 '22 05:12

Steven


1 Answers

You can leave out the minor or dot versions when linking to the google CDN copies, like this:

  • jQuery 1.4.2: http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
  • Latest 1.4.x: http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js
  • Latest 1.x.x: http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js

Given this flexibility, I much prefer the remote version, it's quick to get bug fixes by just changing the URL and the file's loaded faster (in most cases) because it's pulled from another domain (and not in the count of parallel requests to your domain). There are other related questions on this, here and here.

like image 200
Nick Craver Avatar answered Dec 03 '22 23:12

Nick Craver