Technology Review

Tech Review. All things .NET

jQuery

jQuery is a multi-browser JavaScript library designed to simplify the client-side scripting of HTML. It was released in January 2006 at BarCamp NYC by John Resig.

It is currently developed by a team of developers led by Dave Methvin. Used by over 55% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use today. jQuery is free, open source software, licensed under the MIT License. jQuery's syntax is designed to make it easier to navigate a document, select DOM elements, create animations, handle events, and develop Ajax applications. jQuery also provides capabilities for developers to create plug-ins on top of the JavaScript library. This enables developers to create abstractions for low-level interaction and animation, advanced effects and high-level, theme-able widgets. The modular approach to the jQuery library allows the creation of powerful dynamic web pages and web applications. Microsoft and Nokia have announced plans to bundle jQuery on their platforms. Microsoft is adopting it initially within Visual Studio for use within Microsoft's ASP.NET AJAX framework and ASP.NET MVC Framework while Nokia has integrated it into their Web Run-Time widget development platform. jQuery has also been used in MediaWiki since version 1.16.

Features


jQuery includes the following features:
  • DOM element selections using the multi-browsers open source selector engine Sizzle, a spin-off out of the jQuery project
  • DOM traversal and modification (including support for CSS 1-3)
  • DOM manipulation based on CSS selectors that uses node elements name and node elements attributes (id and class) as criteria to build selectors
  • Events
  • Effects and animations
  • AJAX
  • Extensibility through plug-ins
  • Utilities - such as user agent information, feature detection
  • Compatibility methods that are natively available in modern browsers but need fall backs for older ones - For example the inArray() and each() functions.
  • Multi-browser (not to be confused with cross-browser) support.


Usage styles


jQuery has two usage styles:
  • Via the $ function, which is a factory method for the jQuery object. These functions, often called commands, are chainable as they all return jQuery objects.
  • Via $.-prefixed functions. These are utility functions, which do not work on the jQuery object per se.
Typically, access to and manipulation of multiple DOM nodes begins with the $ function being called with a CSS selector string, which results in a jQuery object referencing matching elements in the HTML page. This node set can be manipulated by calling instance methods on the jQuery object, or on the nodes themselves. For example:
$("div.temp").add("p.quarter").addClass("yellow").slideDown("slow");

                            
This line finds the union of all div tags with class attribute temp and all p tags with CSS class attribute quarter, adds the class attribute yellow to each matched element, and then increases their height with an animation. The $ and add functions affect the matched set, while the addClass and slideDown affect the referenced nodes. The following script automatically checks whether the jQuery file is included. If not, it appends a jquery reference to the head section
if(!(window.jQuery && window.jQuery.fn.jquery == '1.7.1')) {
  var s = document.createElement('script');
  s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');
  s.setAttribute('type', 'text/javascript');
  document.getElementsByTagName('head')[0].appendChild(s);
}

The methods prefixed with $. are convenience methods or affect global properties and behaviour. For example, the following is an example of the map function called each in jQuery:
$.each([1,2,3,4,5], function(){
  document.write(this + 3);
});

This writes "45678" to the document. It is possible to perform browser-independent Ajax queries using $.ajax and associated methods to load and manipulate remote data.
$.ajax({
  type: "POST",
  url: "sample.aspx",
  data: "name=Joe&location=Miami"
}).done( function(msg){
  alert( "Data Saved: " + msg );
}).fail( function( xmlHttpRequest, statusText, errorThrown ) {
  alert(
    "Your form submission failed.\n\n"
      + "XML Http Request: " + JSON.stringify( xmlHttpRequest )
      + ",\nStatus Text: " + statusText
      + ",\nError Thrown: " + errorThrown );
});

This example posts the data name=Joe and location=Miami to sample.aspx on the server. When this request finishes successfully, the success function is called to alert the user.

jQuery Plug-Ins


Because of jQuery's architecture, other developers can use its constructs to create plug-in code to extend its functionality. Currently there are thousands of jQuery plug-ins available on the web that cover a wide range of functionality such as Ajax helpers, webservices, datagrids, dynamic lists, XML and XSLT tools, drag and drop, events, cookie handling, modal windows, even a jQuery-based Commodore 64 emulator. An important source of jQuery plug-ins is the Plugins sub-domain of the jQuery Project website. However, in an effort to rid the site of spam, the plugins in this subdomain were accidentally deleted in December 2011. The new site will include a GitHub-hosted repository, which will require developers to resubmit their plugins and to conform to new submission requirements. There are alternative plug-in search engines like jquer.in that take more specialist approaches, such as listing only plug-ins that meet certain criteria (e.g. those that have a public code repository). The tutorials page on the jQuery site has a list of links to jQuery plug-in tutorials under the "Plugin development" section.

jQuery Resources

About me
Published
Copyright © 2013 - 2014 Nils-Holger Nägele