.NET

An Overview of JavaScript Libraries

By Dino Esposito, June 16, 2008

A quick look at Dojo, jQuery, and PrototypeJS

The implementation of AJAX features requires the execution a lot of JavaScript from within the client browser. Typically, the code will issue remote calls to some URL and arrange some nice user interface effects around your page. It may perform data binding, refresh templates, do some polling, or perhaps offer client-side caching. Who does write this JavaScript code? Unless you opted for a tool that automatically generates user interface code for you (for instance, Google Web Toolkit or the upcoming Microsoft Volta), the pleasure of writing lines and lines of JavaScript code is reserved to you. Not too bad if it were only plain application logic code. Developers don't usually mind to implement use-cases as long as they have proper tools to script and combine. Enter JavaScript AJAX libraries.

There are quite a few JavaScript frameworks available out there that offer AJAX and UI-related features. You can find a comparison of them on Wikipedia here. In this article, I briefly go through three of the most popular libraries: Dojo, jQuery, and PrototypeJS.

It should be noted that all libraries work in a cross-browser manner, as this is a key requirement for any such library or toolkit designed to simplify and possibly speed up the development of AJAX front-ends and Web sites.

In Dojo you find prepackaged components for common user interface elements such as menus, tabs, tooltips, date-picker, time-picker, treeview with drag-and-drop, readymade forms, input validation. Dojo also offers sortable tables, 2D vectorial graphics, animated effects like fades and wipes and it also abstracts the popular XMLHttpRequest object to a custom component. This component automatically falls back to using an IFRAME when this is convenient. Dojo also provides local data storage that extends cookies and leverages any browser's capabilities. The abstract model works with Internet Explorer, Firefox, and Safari.

A lightweight JavaScript library, jQuery comes with all of its features packaged in a single file. It offers DOM and CSS manipulation and navigation, event handling, nice user interface effects, and AJAX capabilities. A specialty of the jQuery house is the $ function that is essentially a factory for the root jQuery object. You typically use the $ function with a CSS selector and grab a few DOM elements in the page. Because the returned object is another jQuery object, you can chain new commands to the object up to building an effective workflow. The jQuery library excels when it comes to animating your HTML. Quite simply, the animate function -- the animation engine -- works by varying numeric style values over time. In this way, you can animate size, position or visibility of an element. For more on jQuery, see Getting Started with jQuery, by Bear Bibeault, Dave Crane,and Jord Sonneveld, with Ted Goddard.

The PrototypeJS library also comes in the form of a single JavaScript file and is distributed either as a standalone product or as part of larger projects, including Ruby on Rails and Script.aculo.us. In addition to providing an abstraction over XMLHttpRequest, the library offers an emulation of classes and thus a flavor of object-orientation. There's no magic or special trick-the JavaScript prototype property is used. It is the same approach that Microsoft has taken in the client JavaScript library that ships with ASP.NET AJAX. The abstraction over XMLHttpRequest is two-folded: You find a function to receive XML data from a remote call and a function to receive a chunk of HTML to be injected as-is in the existing DOM.

Regardless of the tools you use to do AJAX programming, you need a JavaScript library. But how to choose your JavaScript library? The three libraries that we briefly examined have slightly different goals. Dojo is a richer toolkit and not simply a library. The other two are essentially a collection of functions to simplify and speed up development. The Microsoft's client library, instead, is a small framework: not many quick-and-dirty facilities, not many cross-cutting Web-specific features, but a good emulation of class programming and general facilities for developers

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!