Tuesday, 27 June 2017

Working with JQuery and CDN


by Rahul Maske,

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.

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

<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.
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.

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

<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.

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

5. 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.

6. Write the following lines of code:

<!-- 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.