InfoQ

News

jQuery: A new way to write JavaScript for rich web UI

Posted by Anil Bhatt on Jul 11, 2007 07:32 PM

Community
Java
Topics
Web Frameworks
Tags
AJAX

Created by John Resig in early 2006, jQuery provides the following main features to simplify JavaScript development:

JQuery Selectors

The basic premise in jQuery is a jQuery selector, which can be used to select elements from an HTML DOM and operate on them. For example, the following jQuery code snippet will show an alert message whenever a link on a page is clicked:

$("a").click(function() { alert("You are leaving this page!");});


$("a") is a jQuery selector. In this case, it selects all anchor elements. $ itself is an alias for the jQuery "class", therefore $() constructs a new jQuery object. The click() function is a method of the jQuery object. It binds a click event to all selected elements (in this case, a single anchor element) and executes the provided function when the event occurs.

Off course, you can apply filters if you want the alert to be shown only for certain links.

The traditional JavaScript will accomplish the same by:


<a href="http://www.infoq.com" onclick="alert('You are leaving this page!')">infoQ</a>


With jQuery, we don't need to write an onclick for every single element. We have a clean separation of structure (HTML) and behavior (JavaScript).

Special Effects

Here is another example of a special effect feature in jQuery. The following code snippet looks for all paragraphs that have a class of "surprise", adds the class "shock" to them, then slowly reveals them:

 

$("p.surprise").addClass("shock").show("slow");

 

Ajax Development

A common use of Ajax is to load a chunk of HTML into an area of the page. With jQuery, you simply have to select the element you need and use the load() function. Here's an example that updates some statistics:


$('#stats').load('stats.html');


The latest version, jQuery 1.1.3 has recently been released with DOM traversal over 800% faster than in 1.1.2. Other major enhancements include a re-written event system , with more graceful handling of keyboard events and a re-written special effects system.

Desktop by Kit Davies Posted Jul 12, 2007 4:51 AM
Re: Desktop by Noah Campbell Posted Jul 12, 2007 11:39 AM
JS wrapper for XUL and hta by karan malhi Posted Jul 13, 2007 12:22 AM
  1. Back to top

    Desktop

    Jul 12, 2007 4:51 AM by Kit Davies

    I'd be interested to know if there are any HTML/JS apps that have been written purely for the desktop and local deployment (backed by eg. Google Gears). I was previously sceptical of JS in the browser, but it seems the JS libraries are beginning to mature and I may have to eat my words! Kit

  2. Back to top

    Re: Desktop

    Jul 12, 2007 11:39 AM by Noah Campbell

    On Windows, the .hta is an html application. You'll find it embedded in quite a few place, but perhaps not using the AJAX libraries for effects. It wouldn't be impossible since .hta will invoke an IE browser container.

  3. Back to top

    JS wrapper for XUL and hta

    Jul 13, 2007 12:22 AM by karan malhi

    I was wondering if there is a library which could wrap XUL and hta components. This would really allow to use rich widgets which are browser specific but using a common JS library.

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.