Welcome!

ColdFusion Authors: Yakov Fain, Maureen O'Gara, Nancy Y. Nee, Tad Anderson, Daniel Kaar

Related Topics: AJAX & REA, ColdFusion

AJAX & REA: Article

Dissecting ColdFusion and AJAX

Since AJAX is a combination of technologies you're going to want to know what you're getting into

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

On the first page, the user will enter a search term, then clicks a submit button to get to the second page where the search results are returned. If the user wants to filter or sort the search results it's usually refreshed again and each result is redisplayed in its filtered state.

Lately there's been a movement to help improve the Web by eliminating these constant page refreshes. Macromedia's Rich Internet Application (RIA) push with Flash and Flex has been at the forefront of this effort, however Flex' entry price has made it prohibitive for many ColdFusion Developers. That's where AJAX comes in. It's an alternate way to create a RIA and relies on technologies built into most browsers, making it completely free.

Dissecting AJAX
AJAX stands for Asynchronous JavaScript and XML. It's not a single technology, but rather a combination of different technologies that are used to create a Rich Internet Application feel. You've probably seen examples of this kind of application. Google uses AJAX a lot on such services as maps.google.com. After bringing up a location, you can click and drag the map, zoom in or out, or even bring up directions to and from the location. All this is done without any screen refreshes. You have to admit that the interface is pretty sweet. (Note: Yahoo has a similar mapping application, built entirely in Flex at http://maps.yahoo.com/beta.)

Since AJAX is a combination of technologies you're going to want to know what you're getting into before going any farther, right? I thought so. Here's the list:

  • XHTML: To start with you need a display language. Most resources on AJAX say you have to use XHTML, but regular HTML will work too.
  • XMLHttpRequest: The XMLHttpRequest object lets you retrieve data from the server without refreshing the page. It's a JavaScript object implemented in most major browsers. As the XMLHttpRequest object is being called, the user can do other things on the page. This is where the "asynchronous" part of the acronym comes in.
  • iFrames: iFrames are in-line frames and are sometimes used as an alternative to the XMLHttpRequest object. A regular frame will divide the browser window into multiple separate pages, but an in-line frame embeds one page inside another. I won't get into any iFrames details in this article.
  • XML: You all know what XML is, right? As a refresher, it's a way to describe data. If you need more details, read the article in this column from August 05 at http://coldfusion.sys-con.com/read/117667.htm.
  • Document Object Model: The document object model defines your HTML page through a programmer API.
  • JavaScript: I'm sure you all know what JavaScript is too. It's the most common language used for providing client-side functionality to HTML pages.
How do all these different technologies work together using AJAX? Well, it generally works something like this:
  1. First, the user comes to your page and the page loads. Something (depending on what your page does) is loaded and displayed to the user.
  2. The user does something such as clicking a link. Instead of reloading the page (as would happen in a normal Web app), that link fires off a JavaScript function.
  3. The JavaScript function uses XMLHttpRequest object to retrieve XML data from the server.
  4. Then the JavaScript parses the XML Data and through the Document Object Model will change the page without a refresh.
So the user gets to see more data quicker without the annoying page refreshes. That's fantastic, right? Why isn't everyone using this? Well, as with all things, AJAX isn't perfect. There are drawbacks:
  • Development Time: Working with AJAX will bring you back to the old days of Web development, especially if you want any kind of cross-compatibility. There are different syntaxes for creating the XMLHttpRequest object in IE vs Netscape/Firefox, which can lead to extended development times.
  • User Experience: When used properly, an AJAX can enhance the user experience. However, when used improperly it can make it horrendous. Since the page isn't reloading, what happens when the user hits the back button? He'll leave the application altogether. I bet he expected to go to the app's "previous state." What happens when the user bookmarks a page? He'll go the "initial" state of the app. There are workarounds, of course, but they're not trivial to implement.
  • Searches: The single-page nature of the app makes indexing by search engines a tetchy proposition at best.
  • Response Times: The very thing that works for you (no page reloads) can also work against you. What happens if you're retrieving a lot of data from the server? And the user is given no indication that something is happening.
  • JavaScript: JavaScript must be enabled for an AJAX app to work. If JavaScript is disabled, your user isn't going to have a lot of fun with an AJAX app.
  • Accessibility: AJAX is, most likely, not going to meet accessibility guidelines. If this is a goal of your project you're going to have to provide a fallback option. That means developing two paths to the same goal.
There are a few real simple examples of AJAX that you've probably used or seen that existed long before the term AJAX was conceived. Have you ever rolled your mouse over a graphic navigation button only to have that graphic change? JavaScript rollovers can be considered a precursor to AJAX. They do use JavaScript and they do change the state of the page. Have you turned on or off a DHTML layer on a page based on some selection by the user? This is another example of AJAX. If an application has a select box with an "other" option in it I'll often use DHTML to display an input text box if "other" is selected. If other isn't selected the input box is hidden. Both of these are simple examples of changing the page without a screen refresh. They don't go out to the server to get data, but they still fit the RIA paradigm.

Putting This All Together
Let me show you how this all comes together, so you can actually do something useful. The first step in the process is to initialize the XMLHttpRequest object. Unfortunately, there's not consistent way to do this across browsers:

<script language="javascript" type="text/javascript">
var request = false;
try {
     request = new XMLHttpRequest();
}
catch (trymicrosoft) {
   try {
     request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (othermicrosoft) {
   try {
     request = new ActiveXObject("Microsoft.XMLHTTP");
   }
    catch (failed) {
     request = false;
    }
  
}
</script>

More Stories By Jeffry Houser

Jeffry is a technical entrepreneur with over 10 years of making the web work for you. Lately Jeffry has been cooped up in his cave building the first in a line of easy to use interface components for Flex Developers at www.flextras.com . He has a Computer Science degree from the days before business met the Internet and owns DotComIt, an Adobe Solutions Partner specializing in Rich Internet Applications. Jeffry is an Adobe Community Expert and produces The Flex Show, a podcast that includes expert interviews and screencast tutorials. Jeffry is also co-manager of the Hartford CT Adobe User Group, author of three ColdFusion books and over 30 articles, and has spoken at various events all over the US. In his spare time he is a musician, old school adventure game aficionado, and recording engineer. He also owns a Wii. You can read his blog at www.jeffryhouser.com, check out his podcast at www.theflexshow.com or check out his company at www.dot-com-it.com.

Comments (16) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Jeff Houser 12/19/06 09:33:05 PM EST

Christopher,

I have no access to the "backend" world of sys-con; I just write and submit articles.

I can re-iterate that it was written last January for the February issue of CFDJ. I can't comment on the publishing (or re-publishing) of it w/ a new date. It appears that sys-con corrected the mistake. Nor can I comment on what gets e-mailed to subscribers and when. None of that is anything I am involved with.

You are right that there is no ColdFusion in the article, and as such the title is misleading. It's probably something I should have caught and corrected in the proof-read. But, I'm not perfect so you'll have to accept my apology for that.

A lot of articles for my column (which is a beginner's column) have no direct relation to CF, but rather deal with topics that affect ColdFusion developers. This article was written for an issue with an editorial focus on AJAX. Even though it was not CF-specific, I still consider the topic relevant to CF Developers.

I'm sorry you didn't enjoy the article.

Christopher Jordan 12/19/06 06:45:33 PM EST

Jeff,

Try typing "Dissecting ColdFusion and AJAX" into Google and then looking at the cached page. You'll see what I saw before you (or your editor) corrected their mistake. The article says it published Nov. 1, 2006.

Nice try.

If your intent was to write an article that guided the user through the mechanics of AJAX, then don't title your article "Dissecting ColdFusion and AJAX". The title is extreemly misleading. You said very, very little about ColdFusion or how it plays with Ajax. Your RSS Feed example seemed to have nothing to do with ColdFusion (this is the ColdFusion Developer's Journal, right?)

A let down of an article to be sure. Perhaps you guys ought to have a system indicating the level at which you will be addressing topics. Beginner, intermediate, advanced.

Also, as I asked your duty editor before, who's bright idea was it to hit people's inbox with old information? Running out of things to write about that are new?

"Hey, brand new for you! An article we dug up from six months ago! :)"

I thought I subscribed to this publication in order to get new, cutting edge information concerning ColdFusion. Not so I could read articles that are months old. Sure, the information in this particular article isn't really "outdated", but it's close neighbors with worthless. I don't know a single soul who codes ajax without using some javaScript library like Dojo, Prototype, or jQuery.

You want to do an article on something new, fresh and bleeding edge? Write an article on jQuery.

I'm done.
Merry Christmas. :o'

Jeff Houser 12/19/06 06:01:45 PM EST

Christopher,

The article was written for the February issue of CFDJ, my initial draft is dated from 11 months ago. As the duty editor pointed out, the article has been on the site since May 2006, I'm not sure why you think it was posted in November.

That said, I think its important to understand the mechanics behind "AJAX" and that was the focus of the article. Yes, there are plenty of frameworks and script libraries as you mentioned, but if I had to write another beginner's article on the subject, I'd concentrate again on underlying mechanics of AJAX, not on the frameworks.

Duty Editor 12/19/06 03:39:13 PM EST

Christopher,

Thanks for the feedback. This article was chosen as part of a "Best of 2006" program, in which we wished to recognize those writers whose work was enjoyed in the course of '06 by our readers. 18,607 people have enjoyed this article since it was first published on March 8, 2006.

Sorry for any misunderstanding. For up-to-the-minute coverage of AJAX, you no doubt are aware of AJAXWorld Magazine, http://ajax.sys-con.com, which is updated daily - sometimes hourly.

Duty Editor
SYS-CON Media

Christopher Jordan 12/19/06 02:53:54 PM EST

Where the hell have you been? Under a rock? The first page of your article made you seem so completely out of touch that I didn't even want to read the second page. I realize this article was published on Nov. 01 of this year, but dude, c'mon, there were heaps of things happening last month to help ease writing ajax and javascript. YUI? Dojo? Prototype? ... and my favorite: jQuery!

Gimme a break. Why am I just now (Dec. 19, 2006) getting an email about an article written over a month ago? Get on the ball guys... or get left behind

CFDJ News Desk 10/30/06 04:45:44 PM EST

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

SYS-CON Brazil News Desk 10/30/06 08:25:51 AM EST

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

SYS-CON India News Desk 10/30/06 06:41:06 AM EST

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

CFDJ News Desk 10/29/06 10:00:50 PM EST

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

j j 09/27/06 11:40:08 AM EDT

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

SYS-CON Italy News Desk 09/25/06 07:53:54 PM EDT

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

CFDJ News Desk 09/25/06 03:34:25 PM EDT

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

AJAXWorld News Desk 09/25/06 09:07:48 AM EDT

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

SYS-CON Belgium News Desk 04/21/06 11:09:01 AM EDT

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

SYS-CON Italy News Desk 04/20/06 06:57:22 PM EDT

One of the most annoying things about the Web page paradigm is that you have to reload the page whenever you want to do something on the server side. If you want to do a site search, it's sprinkled across two pages.

John Dobson 04/20/06 06:24:18 PM EDT

A very helpful article. It might be useful to add that the getRSS() function must actually be invoked somewhere, for example in the onLoad attribute of the BODY tag.

In the online edition of the article, there is a closing curly brace missing at the end of the first block of JavaScript.