Including the jQuery Library Code in an HTML Page Problem - J Query

Problem
You want to use the jQuery JavaScript library on a web page.

Solution

There are currently two ideal solutions for embedding the jQuery library in a web page:

  • Use the Google-hosted content delivery network (CDN) to include a version of jQuery (used in this chapter).

  • Download your own version of jQuery from jQuery.com and host it on your own server or local filesystem.

Discussion

Including the jQuery JavaScript library isn’t any different from including any other external JavaScript file. You simply use the HTML <script>element and provide the element a value (URL or directory path) for its src="" attribute, and the external file you are linking to will be included in the web page. For example, the following is a template that includes the jQuery library that you can use to start any jQuery project:

Notice that I am using—and highly recommend using for public web pages—the Google-hosted minified version of jQuery. However, debugging JavaScript errors in minified code is not ideal. During code development, or on the production site, it actually might be better to use the nonminified version from Google for the purpose of debugging potential JavaScript errors.
It’s of course also possible, and mostly likely old hat, to host a copy of the jQuery code yourself. In most circumstances, however, this would be silly to do because Google has been kind enough to host it for you. By using a Google-hosted version of jQuery, you benefit from a stable, reliable, high-speed, and globally available copy of jQuery. As well, you reap the benefit of decreased latency, increased parallelism, and better caching.This of course could be accomplished without using Google’s solution, but it would most likely cost you a dime or two.
Now, for whatever reason, you might not want to use the Google-hosted version of jQuery. You might want a customized version of jQuery, or your usage might not require/have access to an Internet connection. Or, you simply might believe that Google is “The Man” and wish not to submit to usage because you are a control freak and conspiracy fanatic. So, for those who do not need, or simply who do not want, to use a Google-hosted copy of the jQuery code, jQuery can be downloaded from jQuery.com and hosted locally on your own server or local file system. Based on the template I’ve provided in this recipe, you would simply replace the src attribute value with a URL or directory path to the location of the jQuery JavaScript file you’ve downloaded.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics