Ajax Libraries: Homegrown or Borrowed

Most of the examples in the book don't use libraries other than the Adding Ajax library, addingajax.js, which we "growed up" as we went along. This leads to another major decision when beginning Ajax development—whether to create your own Ajax library, or use one or more of the many free libraries available on the Internet.

Some libraries such as Prototype provide basic functionality and are quite small. Others, though, can be rather large, and unless you have a real commitment to integrate them into your application, the bandwidth needed for the library isn't justified.

You can mix libraries as long as each uses some form of namespacing to prevent conflicts, and as long as the libraries are using event handlers that don't tromp all over each other or your own code, of course.

As for creating your own library, you can take a minimal, basic library such as addingajax.js and add your own components. One such addition is simplified processes to add and access DOM elements in order to cut down on the amount of code. The following example creates a two-item unordered list, with each item containing an anchor element:

// create unordered list container var ul = document.createElement('ul'); // create two anchor elements var a1 = document.createElement('a'); var a2 = document.createElement('a'); a1.setAttribute('href','http://someurl.com/'); var txt1 = document.createElement('some text'); a1.appendChild(txt1); a2.setAttribute('href','http://someotherurl.com/'); ...

Get Adding Ajax now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.