| By Rob Gonda | Article Rating: |
|
| September 10, 2007 05:15 PM EDT | Reads: |
202,842 |
Trick 2
Observe that when you pass back HTML content to JavaScript, you have to URL-Encode the content (see Listing 7). If you try to pass back the HTML tags without encoding it, the AJAX calls will break without notice or error messages. In the receiving counterpart, you need to decode the HTML and this is achieved by using the unescape function as shown in Listing 6.
The drawback of this method is that it completely kills search engine friendliness. Search engines will not execute JavaScript calls and will not index your content. As a rule of thumb, I try to keep all front-end related content as SEO friendly as possible. You can use these examples for back-end related purposes. For example, gmail was written completely in AJAX. Notice that you can read and write e-mails and the site will never refresh and the speed is different over hotmail or any other Web mail application.
Table Example
Next, I'll cover looping over an AJAX record set and populating tables. Although you could generate the table and send the rendered HTML by using my previous content example, by using this technique, you'll delegate the load to the client browser instead of keeping it in the server.
I will introduce now a few DOM functions: createElement, createTextNode, setAttribute, appendChild, and removeChild. The concept here is to receive a recordset from a ColdFusion function and create the HTML by using DOM functions instead of the HTML tags.
In this example, I'll only demonstrate the JavaScript part that builds the table. If you look at Listing 8, you'll see an empty table with an ID in the "tbody" tag. This "tbody" tag is extremely useful when you have additional elements such as column headers in the "thead" tag. The "tbody" allows us to reference that section directly when adding and deleting elements.
The logical process that I follow in my pages is showing a "please wait" DIV when the user submits the request. At this point I make the AJAX call, send the request parameters, and wait for the response. The response will trigger the callback function, which immediately executes the "clear_table" function. This function loops over each row in the "tbody" and calls the DOM "removeChild" function. This will clear the results from the previous call. After I finished emptying the table, we proceed to populate it with the new recordset. If you look at the "showTable" function, you'll find two nested loops: one for each row and one for each column. Looping over each column is optional and up to you. Traditional ColdFusion programmers may prefer to loop over each row and manually code each column to their needs. Personally, I like to avoid having to write the same code over and over if I can help it. In my example, I created an array called "r" that contains the columns that I want to display. After you finish populating the table with the new recordset, you may remove the "please wait" message and the process is over.
Populating the table is actually rather simple: first I loop over each row from the recordset and create a "TR" element by using createElement('tr'); then in the nested loop I create the "TD" element for each column by using createElement('td'). The text is then evaluated in the following line "currenttext=document.createTextNode(eval('o.' + r[i] + '[j]'));". Here the "eval" function is resolving the recordset, then the column name, and then the position. For instance, the first row, first column, will read "o.uname[0]".
Creating the links is not as simple as typing "<a>". You will need to create an "A" element by using createElement('a'), and then use the setAttribute to add the href attribute.
After all is said and done, all that's left to do is to append each element to its parent. First you append the text node to the "a" element, then the "a" to the "td", then the "td" to the "tr", and finally the "tr" to the "tbody".
CFAJAX includes some utilities that facilitate this process but, the truth is, you will need to learn DOM JS if you really want to take control. For example, the example in Listing 8 only adds a link to the first column, and all the rest are just text.
Related Options Example
My last example will explain a useful built-in function with CFAJAX: related options (see Listing 9). How many times have you had to load enormous arrays of options to your HTML template to achieve this? Wouldn't it be great if you could load the dependent options only after the main option has been selected? CFAJAX includes two handy functions: "removeAllOptions" and "addOptions". These two functions will drive the JavaScript work to achieve related options to zero.
I hope the community doesn't mind, but I'll use the example from their site to demonstrate how this is achieved.
First, we'll have a brand dropdown that will call a JavaScript function called "getBrand" on its "onChange" trigger. All this function does is send ColdFusion the value of the current selected brand, and ColdFusion will return an array of values.
The array actually contains value pairs, which are the key, and the value separated by commas. It's important to pay attention to the hint value of the ColdFusion function; in this example, it's hint="type='keyvalue' jsreturn='array'", which is actually used in the callback function to set the expected value types. You can tell JavaScript the complex type of the return, set it to receive value pairs, and optionally set the delimited of the pairs. By default, it is expecting a comma for the delimiter.
The callback function cannot get any simpler. "getBrandResults" performs only two functions: DWRUtil.removeAllOptions("model"), which removes all current options from the model select component, and DWRUtil.addOptions("model", modelArray, "KEY", "VALUE"), which add the returned array to the model select component.
Conclusion
I hope you learned how AJAX could create a better user experience on your site. I covered passing complex objects, using innerHTML to modify the view layer after it has been rendered, drawing dynamic content, manipulating tables, and using related select options. AJAX is extremely fast, secure, and allows for advanced functionality that synchronous HTML does not.
Published September 10, 2007 Reads 202,842
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Rob Gonda
Rob Gonda is an industry visionary and thought leader, speaks on emerging technologies conferences nationwide, and combines unique approaches to technology and marketing strategies. He is the former Editor-in-Chief of the AJAX Developer’s Journal, an Advanced Certified Coldfusion Developer, member of the Adobe Community Experts, frequent contributor to the CFDJ and ADJ, co-author of Real-World AJAX: Secrets of the Masters, author of AjaxCFC, holds a BS in computer science and engineering and an MBA with a specialization in entrepreneurship. Rob recently joined Sapient from ichameleon/group/ where he was a founding partner and chief technical officer. He is part of the global technology leadership team, and brings with him over ten years of experience in web development and 360 marketing campaigns for clients such as Adobe, Coca-Cola, Guinness, Toyota, Taco Bell, NBC, and others. He specializes in emerging technologies, marketing strategy, social media, and he is currently fascinated with rich internet applications, service oriented architecture, mobile, agile methodology, automation, behavioral targeting, multi-channel synergy, and identifying new trends. Rob’s mission is to develop forward-thinking expertise that will ensure clients are always on par with rapidly changing technologies and maintain its ethos of evolving. You can reach him at rob[at]robgonda[dot]com and read his blog is at http://www.robgonda.com
![]() |
Swamy 04/21/08 03:57:36 PM EDT | |||
Hi, I need a basic clarification. will Ajax can be implemented in ColdFusion 5 version(on IIS/oracle 9i). If yes, please help me by providing some information like How to setup engine files and how to get it worked in CF5. |
||||
![]() |
karthikeyan Palani 01/20/08 03:21:17 AM EST | |||
Hi Rob, |
||||
![]() |
Hello Ajaxer 09/10/07 10:07:01 PM EDT | |||
did u recognize the Ajax interface as you posted your feedback, or you have no clue what Ajax means? |
||||
![]() |
ajaxer 09/10/07 09:36:31 PM EDT | |||
too bad you didnt use AJAX on this site... |
||||
![]() |
SYS-CON Brazil News Desk 04/06/06 10:25:25 AM EDT | |||
It's become very popular lately, even though it's not exactly new. It's been possible to use the concept behind AJAX since browsers introduced the XMLHttpRequest function in 1999. Enjoy Rob Gonda's highly popular ColdFusion Developer's Journal article on AJAX, the hottest software development of 2005, with plenty more heat promised for 2006. |
||||
![]() |
news desk 02/13/06 08:41:32 PM EST | |||
It's become very popular lately, even though it's not exactly new. It's been possible to use the concept behind AJAX since browsers introduced the XMLHttpRequest function in 1999. Enjoy Rob Gonda's highly popular ColdFusion Developer's Journal article on AJAX, the hottest software development of 2005, with plenty more heat promised for 2006. |
||||
![]() |
Rob Gonda 01/23/06 01:35:58 AM EST | |||
Neither FF or IE will allow cross-domain scripting. This is a securiry risk, read more here: There are ways around it like a Server Side proxy, or third party JavaScripts like Ajax Extend (http://ajaxextended.com/) -Rob |
||||
![]() |
Morgan 01/23/06 01:21:16 AM EST | |||
Ok had a little time to kill and decided I'd give Ajax a try and see what all the hype was about. Downloaded and installed cfAjax folowed install directions step by step. all seemed fine untill I tried to run one of the sample applications. Then I got the following error. Permission denied to call method xmlhttpRequest.open I decided to load a full version of ajax and still got same error In looking for an answer I keep seeing the following as the culprit with no real solution. This error occurs when you try to make requests off-domain. which in pouring over the code I cant find anywher that the examples are making calls to off-domain. So lets cover the basics.. yes error is same in both ie and moxilla no I'm not using a local file (ie //file/ajax/example) no I dont have a firewall running between my work station and my development server yes both machines are windows running IIS yes the url has http (ie http://myDevelopmentServer/ajax/examples/text.htm yes I created a virtual host for ajax in both IIS and my CF Administrator So I guess that covers it Any help would be great |
||||
![]() |
Robert Blackburn - DevBlog 01/17/06 09:52:59 PM EST | |||
Trackback Added: What Is AJAX?; While I am on the topic of CFDJ articles, there is another one titled What Is AJAX?. This one begins a lot like my earlier p... |
||||
![]() |
Baz Web Development: Ajax, FastCGI, Joomla 12/31/05 05:34:46 AM EST | |||
Trackback Added: Another “What Is Ajax” Introduction; What Is Ajax? |
||||
![]() |
Rob Gonda 12/24/05 01:51:46 AM EST | |||
Mohan, I am glad I could help; I definitely tried to make the article so easy and straight forward so everyone can get AJAX started. This will be one of the major topics for 2006. To stay up to date with AJAX, please check my blog (www.robgonda.com) http://www.robgonda.com Best, -Rob |
||||
![]() |
mohan 12/23/05 08:20:53 PM EST | |||
i am new to AJAX this is a wonderful article. After seeing this article i am going to implement CFAJAX in our (production) search form. thank you veryyyyyyyyyy much regards mohan |
||||
![]() |
Rob Gonda 12/15/05 11:56:06 PM EST | |||
Perhaps, nothing really prevented anyone from using it; Jesse James Garrett just had to come up with the idea/concept. By the way, don't miss Jesse at the Ajax Seminar (www.ajaxseminar.com/) |
||||
![]() |
Rob Gonda 12/14/05 09:25:12 PM EST | |||
It is indeed; AJAX involves combining any server scripting language with client side, hence, JavaScript. I can see how you could probably automate a two related select boxes without writing JavaScript, but certainly not an RIA or Web 2.0 application. |
||||
![]() |
Editor 12/14/05 06:53:44 PM EST | |||
SYS-CON will soon launch a dedicated online AJAX section but it will not be called "AJAX Developer's Journal" or "AJAX Magazine." Thanks and best regards. P.S. Please stay tuned for SYS-CON's complete educational offerings to be announced soon. |
||||
![]() |
Rob Gonda 12/13/05 10:54:35 PM EST | |||
I just looked at the source code attached to the article, and you're right, it's missing model.cfm. |
||||
![]() |
no comment 12/13/05 04:50:31 PM EST | |||
I have cfajax working, but your example model.cfm doesn't do anything. What am I doing wrong? |
||||
![]() |
news desk 12/11/05 08:17:11 PM EST | |||
So What's AJAX? (Part One of Two) It's become very popular lately, even though it's not exactly new. It's been possible to use the concept behind AJAX since browsers introduced the XMLHttpRequest function in 1999. |
||||
![]() |
ajax 12/11/05 05:09:45 PM EST | |||
So What's AJAX? (Part One of Two) |
||||
![]() |
So What's AJAX? 12/11/05 05:01:05 PM EST | |||
So What's AJAX? (Part One of Two) |
||||
![]() |
CFDJ News Desk 12/11/05 04:41:45 PM EST | |||
So What's AJAX? (Part One of Two) It's become very popular lately, even though it's not exactly new. It's been possible to use the concept behind AJAX since browsers introduced the XMLHttpRequest function in 1999. |
||||
![]() |
Rob Gonda 12/09/05 12:34:48 AM EST | |||
by the way, check out my latest alternative to cfajax. I developed an ajax CFC framework. check it out at http://www.robgonda.com/blog/projects/ajaxcfc/ |
||||
![]() |
Rob 11/26/05 07:06:09 PM EST | |||
For more information please visit http://www.robgonda.com/blog |
||||
![]() |
Rob 11/22/05 11:46:03 PM EST | |||
Just testing this feedback again... Trying to find out exactly what's happening. |
||||
![]() |
ColdFusion Developer's Journal News Desk 10/27/05 09:13:41 PM EDT | |||
So What's AJAX? It's become very popular lately, even though it's not exactly new. It's been possible to use the concept behind AJAX since browsers introduced the XMLHttpRequest function in 1999. |
||||
![]() |
Rob 10/27/05 08:40:25 PM EDT | |||
I already wrote and submitted the second part of the article. It gets more in depth and provides lots of code examples. Cheers |
||||
![]() |
Rob 10/27/05 08:38:08 PM EDT | |||
Adam, I cannot help you with FB as I really dont use it much. I built my own MVC framework, but I was an active model-glue user. I can help you with MG or MII frameworks if it works for you. |
||||
- Kindle 2 vs Nook
- Is Cloud Computing Like Teenage Sex?
- GovIT Expo Highlights Cloud Computing
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Cloud Computing Can Revitalize Your Career as Software Developer
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Yahoo! SVP Shelton Shugar to Discuss Innovation at Cloud Computing Expo
- Virtualization Journal "Readers' Choice Awards" Voting Is Now Open
- Einstein, Sharks and Clouds: IT Security in the Cloud
- Adobe Flex Developer Earns $100K in New York City
- Virtualization Expo Call for Papers Deadline December 15
- Amazon Web Services Database in the Cloud
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Is Cloud Computing Like Teenage Sex?
- 1st Annual GovIT Expo: Letter from the Technical Chair
- Ulitzer News: Search vs New Media
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Confessions of a Ulitzer Addict
- GovIT Expo Highlights Cloud Computing
- Twitter, Linked In, Ning and Ulitzer: Easy Personal Branding Strategy
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- The i-Technology Right Stuff
- Linux.SYS-CON.com Exclusive: Linus Discloses *Real* Fathers of Linux
- After Ubuntu, Windows Looks Increasingly Bad, Increasingly Archaic, Increasingly Unfriendly
- Linus' Top Ten SCO Barbs
- A Closer Look at Damn Small Linux
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Where Are RIA Technologies Headed in 2008?
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- i-Technology Viewpoint: The New Paradigm of IT Buying
- Is Linux Desktop-Ready Yet...or Not?































