Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Introduction to jQuery Ways to Include jQuery in a Project

Werner Chao
PLUS
Werner Chao
Courses Plus Student 1,951 Points

Regarding JQuery CDN

I still don't understand why the instructor included the JQuery CDN in the header: <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>

Why it's included, and how it's different than downloading it? Why need to download it at all?

Thanks!

3 Answers

Hi.

The CDN is a provider of different libraries like jQuery.

You can include the file using the CDN and this means:

  • You don't need to download the files to your local machine
  • You don't have to include all the required files that are needed for a plugin to function properly
  • If the library is updated you only need to change one line of code

If you download the files to your local machine:

  • You have to include all the dependencies
  • Normally you have to keep a specific folder structure
  • If a library is updated then you have to redownload the files and change the code to include the new updated version
  • When you include a local copy of the library the plugin will always be availible -> if CDN goes down for any reason your plugin still functions like it should (but I never saw the CDN go down so far.

To sum it up: it's all up to you how you want to include the libraries and plugins.

Hope this clears up a bit.

Regards,

nVVEBd

Hi Werner Chao,

jQuery is a JavaScript library. So if you use the jQuery in your site, you need to link it to the library in some way, so that the code is interpreted.

Effectively, you shouldn't really notice any difference between downloading jQuery, or linking to the CDN.

I found one quote from W3C which lists one advantage of the latter option:

Many users already have downloaded jQuery from Google or Microsoft when visiting another site. As a result, it will be loaded from cache when they visit your site, which leads to faster loading time. Also, most CDN's will make sure that once a user requests a file from it, it will be served from the server closest to them, which also leads to faster loading time.

source: http://www.w3schools.com/jquery/jquery_intro.asp

Hope that helps,

Ede