How to add jQuery to your site
By Andreas Blixt. Updated 2 years, 10 months ago. 18,205 unique views.
There are several ways of adding jQuery to your site.
Use Google's servers
Google actually hosts jQuery on their servers and let you fetch it from them, no strings attached. The benefits of getting it from Google's servers are several. More about them later.
Including jQuery from Google's servers is very easy. We recommend that you include it in the bottom of the page, because this will make your page load faster. Open the HTML for your site and locate the end of the page body (
</body>). Put the following just before the
This will get the latest bug-fix release of jQuery 1.5 (as of this writing 1.5.2). You can also get, for example, version 1.3.2 by changing 1.5 in the URL above to 1.3.2.
Alright, you're all good to go!
Why should I use Google's servers?
- Many web browsers will limit the number of downloads per domain. Since you're getting jQuery from another domain, that's one more file that can be downloaded simultaneously.
- Since a lot of other sites use the same method, your visitors may already have downloaded jQuery from Google once. This means that jQuery is cached on their computer and no download will be required.
- The file is on a so-called CDN (Content Delivery Network), which means that the file is spread across a lot of servers around the world, and the server closest to your visitor will be chosen for the download. This results in less time waiting for the download to start.
- Depending on how you include the file, you can get Google to provide you with the latest bug-fix version of jQuery, so you don't have to update it yourself.
Host the file yourself
You can also host the file yourself. You can download the latest version of jQuery from their site. Make sure you choose the Production version. This version is much smaller and will make your site load faster. If the file opens in your browser instead of being downloaded, just right click somewhere on the page and choose Save As… or select File and then Save As…. Put the file with your other web site files.
All you need to do now is to include jQuery on your site. It's the same procedure as for including the script from Google, but a different path. Put the following before the
Note that you may have downloaded a later version of jQuery, so make sure that the filename is the same as what you put in the