As with all projects that require the use of jQuery, we must start somewhere—no doubt you've downloaded and installed jQuery a thousand times; let's just quickly recap to bring ourselves up to speed.
If we browse to http://www.jquery.com/download, we can download jQuery using one of the two methods: downloading the compressed production version or the uncompressed development version. If we don't need to support old IE (IE6, 7, and 8), then we can choose the 2.x branch. If, however, you still have some diehards who can't (or don't want to) upgrade, then the 1.x branch must be used instead.
To include jQuery, we just need to add this link to our page:
<script src="http://code.jquery.com/jquery-X.X.X.js"></script>
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account from http://www.packtpub.com. If you purchase this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Here, X.X.X
marks the version number of jQuery or the Migrate plugin that is being used in the page.
Conventional wisdom states that the jQuery plugin (and this includes the Migrate plugin too) should be added to the <head>
tag, although there are valid arguments to add it as the last statement before the closing <body>
tag; placing it here may help speed up loading times to your site.
This argument is not set in stone; there may be instances where placing it in the <head>
tag is necessary and this choice should be left to the developer's requirements. My personal preference is to place it in the <head>
tag as it provides a clean separation of the script (and the CSS) code from the main markup in the body of the page, particularly on lighter sites.
I have even seen some developers argue that there is little perceived difference if jQuery is added at the top, rather than at the bottom; some systems, such as WordPress, include jQuery in the <head>
section too, so either will work. The key here though is if you are perceiving slowness, then move your scripts to just before the <body>
tag, which is considered a better practice.
A useful point to note at this stage is that best practice recommends that CDN links should not be used within a development capacity; instead, the uncompressed files should be downloaded and referenced locally. Once the site is complete and is ready to be uploaded, then CDN links can be used.
If you've used any version of jQuery prior to 1.9, then it is worth adding the jQuery Migrate plugin to your pages. The jQuery Core team made some significant changes to jQuery from this version; the Migrate plugin will temporarily restore the functionality until such time that the old code can be updated or replaced.
The plugin adds three properties and a method to the jQuery object, which we can use to control its behavior:
Adding the plugin is equally simple—all you need to do is add a link similar to this, where X
represents the version number of the plugin that is used:
<script src="http://code.jquery.com/jquery-migrate-X.X.X.js"></script>
If you want to learn more about the plugin and obtain the source code, then it is available for download from https://github.com/jquery/jquery-migrate.
We can equally use a CDN link to provide our jQuery library—the principal link is provided by MaxCDN for the jQuery team, with the current version available at http://code.jquery.com. We can, of course, use CDN links from some alternative sources, if preferred—a reminder of these is as follows:
Google (https://developers.google.com/speed/libraries/devguide#jquery)
Microsoft (http://www.asp.net/ajaxlibrary/cdn.ashx#jQuery_Releases_on_the_CDN_0)
jsDelivr (
http://www.jsdelivr.com/#%!jquery
)
Don't forget though that if you need, we can always save a copy of the file provided on CDN locally and reference this instead. The jQuery CDN will always have the latest version, although it may take a couple of days for updates to appear via the other links.