HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [280]
♦ It’s lightweight. You need to include a reference to your library in every file that needs it. The entire jQuery library fits in 55K, which is smaller than many image files. It won’t have a significant impact on download speed.
♦ It supports a flexible selection mechanism. jQuery greatly simplifies and expands the document.getElementById mechanism that’s central to DOM manipulation.
♦ It has great animation support. You can use jQuery to make elements appear and fade, move and slide.
♦ It makes AJAX queries trivial. You’ll be shocked at how easy AJAX is with jQuery.
♦ It has an enhanced event mechanism. JavaScript has very limited support for events. jQuery adds a very powerful tool for adding event handlers to nearly any element.
♦ It provides cross-platform support. The jQuery library tries to manage browser-compatibility issues for you, so you don’t have to stress so much about exactly which browser is being used.
♦ It supports user interface widgets. jQuery comes with a powerful user interface library, including tools HTML doesn’t have, like drag-and-drop controls, sliders, and date pickers.
♦ It’s highly extensible. jQuery has a plug-in library that supports all kinds of optional features, including new widgets and tools like audio integration, image galleries, menus, and much more.
♦ It introduces powerful new programming ideas. jQuery is a great tool for learning about some really interesting ideas like functional programming and chainable objects. I explain these as you encounter them, mainly in Chapter 4 of this minibook.
♦ It’s free and open source. It’s available under an open-source license, which means it costs nothing to use, and you can look it over and change it if you wish.
♦ It’s reasonably typical. If you choose to use a different AJAX library, you can still transfer the ideas you learned in jQuery.
Installing jQuery
The jQuery library is easy to install and use. Follow these steps:
1. Go to http://jquery.com.
2. Download the current version.
As of this writing, the most current version is 1.4.2.
You may be able to choose from a number of versions of the file. I recommend the minimized version for actual use. To make this file as small as possible, every unnecessary character (including spaces and carriage returns) was removed. This file is very compact but difficult to read. Download the nonminimized version if you want to actually read the code, but it’s generally better to include the minimized version in your programs.
3. Store the resulting .js file to your working directory.
jQuery-1.4.2.min.js is the current file.
To incorporate the library in your pages, simply link to it as an external JavaScript file:
Be sure to include the preceding code before you write or include other code that refers to jQuery.
Importing jQuery from Google
Easy as it is to add jQuery support, you have another great way to add jQuery (and other AJAX library) support to your pages without downloading anything. Google has a publicly available version of several important libraries (including jQuery) that you can download from its servers.
This has a couple of interesting advantages:
♦ You don’t have to install any libraries. All the library files stay on the Google server.
♦ The library is automatically updated. You always have access to the latest version of the library without making any changes to your code.
♦ The library may load faster. The first time one of your pages reads the library from Google’s servers, you have to wait for the full download, but then the library is stored in a cache (a form of browser memory) so that subsequent requests are essentially immediate.
Here’s how you do it:
Essentially, loading jQuery from Google