Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / Javascript

What is jQuery and How to Start using jQuery?

Rate me:
Please Sign up or sign in to vote.
4.58/5 (34 votes)
22 Feb 2011CPOL8 min read 345.2K   77   27
An introduction to jQuery and how to use it

Introduction

jQuery is not a language, but it is a well written JavaScript code. As quoted on official jQuery website, "it is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development".

What is jQuery?

jQuery is not a language, but it is a well written JavaScript code. As quoted on official jQuery website, "it is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development."

In order to work with jQuery, you should be aware of the basics of JavaScript, HTML and CSS.

It was released in January 2006 at BarCamp NYC by John Resig.

Licensing

It is free, open source software Dual-licensed under the MIT License and the GNU General Public License. Microsoft has integrated jQuery officially into its IDE Visual Studio 2010 and jQuery intellisense is available in Visual Studio 2010 now.

This is the first chapter of my jQuery ebook. This ebook comes with easy to navigate demo app with source code and video tutorials.

Why jQuery?

jQuery is very compact and well written JavaScript code that increases the productivity of the developer by enabling them to achieve critical UI functionality by writing very small amount of code.

  • It helps to improve the performance of the application
  • It helps to develop most browser compatible web page
  • It helps to implement UI related critical functionality without writing hundreds of lines of codes
  • It is fast
  • It is extensible – jQuery can be extended to implement customized behavior

Other advantages of jQuery are:

  • No need to learn fresh new syntaxes to use jQuery, knowing simple JavaScript syntax is enough
  • Simple and cleaner code, no need to write several lines of codes to achieve complex functionality

Where to Download jQuery from?

jQuery JavaScript file can be downloaded from jQuery Official website.

How to Use jQuery?

jQuery usually comes as a single JavaScript file containing everything comes out of the box with jQuery. It can be included within a web page using the following mark-up:

To Load Local jQuery File

HTML
<script type="text/javascript" src="jQuery-1.4.1-min.js"></script>

Ideally, this markup is kept in under <head></head> tag of your web page, however you are free to keep anywhere you want.

Do I need to refer jQuery file both in Master page/base page/template page and content page?

No, master page/base page/ template page basically helps to create consistent layout for the page in the application. In case you have referred the jQuery file in master page/base page/ template page that causes rendering the file in the browser, you do not need to refer jQuery file in the content page again.

In summary, there should not be more than one <script> tag with jQuery file reference in the source code of the rendered web page in the browser.

What is the difference between jQuery-x.x.x.js and jQuery.x.x.x min.js?

In terms of functionality, there is no difference between the jQuery-x.x.x.js and jQuery-x.x.x-min.js (also called minified version). However, this can play a vital role in the performance of the web page.

How does it affect performance?

jQuery-1.4.4.js file size is 178 KB as against its minified version jQuery-1.4.4-min.js that is only 76.7 KB in size. So when your page loads in the client’s browser if you are not using minified version, it loads 178 KB file that takes more time to load than 76.7 KB.

Which Version of jQuery File Should Be Used?

In most of the recent releases so far, the core functionality of jQuery remains the same, however some more cool and better features are added. Ideally, you should use the latest jQuery files available on the jQuery.com website. By doing this, you ensure that your earlier functionality will still work and you can use new features available as part of the new release.

Loading jQuery from CDN

What is CDN?

CDN Stands for Content Distribution Network or also called Content Delivery Network is a group of computers placed at various points connected with network containing copies of data files to maximize bandwidth in accessing the data. In CDN, a client accesses a copy of data nearer to the client location rather than all clients accessing from the one particular server. This helps to achieve better performance of data retrieval by client.

There are two leading CDNs available that host jQuery files.

Microsoft - To load jQuery from Microsoft AJAX CDN

jQuery file can be loaded from Microsoft AJAX CDN. More details are available here. You will need to keep the following tags in your page.

HTML
<script type="text/javascript" language="Javascript" 
	src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.min.js"></script> 

Google - To load jQuery from Google Libraries API

A jQuery file can be loaded from Google CDN. You will need to keep the following tag in your page.

HTML
<script type="text/javascript" language="Javascript" 
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
</script>

Why to Load jQuery File from CDN?

You may ask that if we can load the jQuery file from our own server, why load it from the CDNs. The answer is logical and very simple. The browser behavior is that whenever it loads any webpage, it keeps related files (e.g., JavaScript file, CSS file and Images) used for that page into its cache (also called history). When next time the user browses any web page, browser loads only those files that are new or modified and is not available in the browser cache or history. In this way, browser improves its performance and loads the page.

The possibility is that if more and more websites are using CDNs, the user might have already browsed some other web pages that are using CDNs jQuery file and that file may have into browser cache; so when user browses your page and you are also using CDNs file, the older cached version of jQuery file will be used. In this way, your page will load faster as browser will not have to load the jQuery file for your page again.

The benefits are as follows:

  1. Faster page load as jQuery file need not be downloaded
  2. Saves your bandwidth as jQuery file is not loaded from your server
  3. Scalable - generally CDNs place these files on the servers located at different geographical locations of the world so that they load faster so irrespective of from where your user is browsing your page, your application runs well.

What if the latest jQuery version is available and I am still referring to the older version of jQuery file from CDNs?

Do not worry about it, it’s a general promise made by CDNs that they will remain hosting the older version of the files on the same location where they had initially released; so even if newer version of the files are released, the older version remains there on the CDNs and your web page still works.

How to Load Local jQuery File in Case CDN is Not Available?

Sometimes, it may happen that the CDN you have used to load the jQuery file is not available (it rarely happens, however anything is possible, isn’t it?); in that case you should load your local jQuery file that is available on your server so that all jQuery related functionality still works on your page.

Write the following lines of code:

HTML
<!-- START - jQuery Reference -->
<script type="text/javascript" language="Javascript" 
	src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.min.js"></script>
    <script type='text/javascript'>//<![CDATA[
        if (typeof jQuery == 'undefined') {
            document.write(unescape("%3Cscript 
	src='/Script/jquery-1.4.1.min.js' type='text/javascript' %3E%3C/script%3E"));
}//]]>
</script>
<!-- END - jQuery Reference -->

Replace bolded path with your own jQuery file path on the server. In the above code, the first line tries to load the jQuery file from CDN, if browser could load the file successfully, "jQuery" variable will not be undefined and next script will not run otherwise next script will run that will write the script tag to load the jQuery file from your server.

How to Execute jQuery Code?

There are two ways you may want to execute jQuery codes.

  1. As and when page loads, execute the jQuery code:

    HTML
    <script language="javascript" type="text/javascript">
    $(function () {
    $("#div1").css("border", "2px solid green");
    });
    </script>

    or:

    HTML
    <script language="javascript" type="text/javascript">
    $("#div1").css("border", "2px solid green");
    </script>

    The benefit of executing jQuery code in this way is that it doesn’t wait for the whole page to load completely, so in case you want the user to see the effects as soon as the corresponding elements are loaded, you can use this.

    However the disadvantage is that if the element on which jQuery has to execute has not loaded, then it will error out or you will not get the desired result; so while using this way of executing jQuery code, you will have to make sure that the element on which you want to work with jQuery is loaded first (you can place your jQuery code right after your HTML element).

  2. Execute jQuery only when the complete DOM objects (the complete page has been loaded). You will have to wrap your code in .ready function.

    HTML
    <script language="javascript" type="text/javascript">
    $(document).ready(function () {
    $("#div1").css("border", "2px solid green");
    });
    </script>

    This is the better and safer way to execute jQuery. This makes sure that jQuery code will execute only if complete page has been loaded in the browser so you are rest assured that user will not see any undesired behavior on the page.

As a developer, the decision of where and how to write jQuery code lies on you. I prefer to use the second method as it ensures that my complete page is loaded in the browser and I am ready to play with any element available on the page.

The video of this chapter is available here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
IT Funda Corporation
India India
Solution of .NET Problems - Get hundreds of .NET How to's

Comments and Discussions

 
GeneralMy vote of 5 Pin
mbcrump31-Jul-11 7:27
mentormbcrump31-Jul-11 7:27 
GeneralMy vote of 4 Pin
Yasir Shah4-May-11 1:48
Yasir Shah4-May-11 1:48 
GeneralMy vote of 5 Pin
Mario Majčica24-Feb-11 1:38
professionalMario Majčica24-Feb-11 1:38 
GeneralRe: My vote of 5 Pin
Sheo Narayan25-Feb-11 0:20
Sheo Narayan25-Feb-11 0:20 
GeneralGood teaser! Pin
Bit-Smacker23-Feb-11 8:53
Bit-Smacker23-Feb-11 8:53 
GeneralRe: Good teaser! Pin
Sheo Narayan24-Feb-11 1:29
Sheo Narayan24-Feb-11 1:29 
GeneralDon't load jQuery from ajax.microsoft.com Pin
DaveRRR22-Feb-11 7:56
DaveRRR22-Feb-11 7:56 
GeneralRe: Don't load jQuery from ajax.microsoft.com Pin
Sheo Narayan22-Feb-11 20:52
Sheo Narayan22-Feb-11 20:52 
GeneralRe: Don't load jQuery from ajax.microsoft.com Pin
Sunasara Imdadhusen23-Feb-11 1:21
professionalSunasara Imdadhusen23-Feb-11 1:21 
GeneralMy vote of 4 Pin
Rounak Hasan18-Feb-11 5:49
Rounak Hasan18-Feb-11 5:49 
Generalnice but Pin
Pranay Rana16-Feb-11 23:57
professionalPranay Rana16-Feb-11 23:57 
GeneralRe: nice but Pin
Sheo Narayan22-Feb-11 20:54
Sheo Narayan22-Feb-11 20:54 
As written, this is the first chapter of the jQuery How to's ebook. This ebook comes with easy to navigate demo app with source code and Videos tutorials.

Thanks
--
Thanks
Sheo Narayan

GeneralMy vote of 5 Pin
Monjurul Habib15-Feb-11 8:53
professionalMonjurul Habib15-Feb-11 8:53 
GeneralRe: My vote of 5 Pin
Sheo Narayan17-Feb-11 12:01
Sheo Narayan17-Feb-11 12:01 
GeneralMy vote of 5 Pin
studleylee14-Feb-11 14:11
studleylee14-Feb-11 14:11 
GeneralFormatting Pin
DaveAuld14-Feb-11 7:41
professionalDaveAuld14-Feb-11 7:41 
GeneralRe: Formatting Pin
Sheo Narayan14-Feb-11 12:54
Sheo Narayan14-Feb-11 12:54 
GeneralRe: Formatting Pin
DaveAuld14-Feb-11 13:05
professionalDaveAuld14-Feb-11 13:05 
GeneralFormatting/Tags Pin
Henry Minute14-Feb-11 3:21
Henry Minute14-Feb-11 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.