Nov
24th

Ajax without the X: Asynchronous dynamic content with JavaScript

Thanks for stopping by my personal blog on Marketing Technology! Over 50,000 visitors a month find my content worth returning for, so don't forget to subscribe to the Marketing Technology Blog RSS feed or to the Marketing Technology Email to have new content sent directly to your inbox. You may also find my other business blog helpful, Social Media Domination.

On another site, I read an article on ’simple’ Ajax interaction… but it wasn’t quite so simple. Working with XML can be a pain in the butt for folks who have no experience with parsing XML. I’ve done quite a few sites with very complex dynamic content schemes and avoided XML by utilizing HTTP Requests and passing the content from the target page’s innerHTML to the local page. I learned this method from the book Professional Ajax.

Professional Ajax (Programmer to Programmer)I’m a bit of a hacker, so I’m sure I’ll get some feedback from a bunch of pro developers on why this is a bad idea or how it could be done easier. However, I have to admit that I have had NO problems doing this! I utilize cross-browser HTTPRequest scripts. In the example below I use the Google Map API built-in HTTPRequest. On other sites, I utilize zxml.js, a nice portable script written by the writers of Professional Ajax.

A huge advantage to the method I’m using is the ability to truly segment out every chunk of your application in its own neat, tidy script. This method can also be used with any server-side language and with or without database interaction. It’s easy to troubleshoot and simple to implement.

Here’s an example:

  1. I have a div id=”dialogue” on my page and the style set to hidden:

    <div id="dialogue" style="visibility:hidden"></div>

  2. The div is a popup dialogue that I want to use for many instances, perhaps to add, edit, delete, search results, etc. The dialogue div is initially loaded with CSS and hidden:

    #dialogue {
    width:600px;
    height:340px;
    position:absolute;
    top:100px; left:100px;
    z-index:99;
    background-color: #ccc;
    margin: 10px;
    padding: 10px;
    }

  3. To open the div and dynamically load the content from another subpage hidden in my site, I just use a simple Javascript function:

    function openDiv(what,i,action) {
    //Uses Google Maps API Http Request
    var request = GXmlHttp.create();
    var obj = document.getElementById("dialogue");
    request.open("GET","get"+what+".php?id="+i+"&action="+action, true);
    request.onreadystatechange = function() {
    if (request.readyState == 4) {
    obj.innerHTML=request.responseText;
    obj.style.visibility = "visible";
    }
    }
    request.send(null);
    }

  4. Now I can put links throughout my page that allow me to call the content to the dialogue. Examples:

    To bring up my Admin screen, I have a page called “getAdmin.php”, the link:

    <a href="javascript:openDiv(’Admin’,0);" title="Open Admin">Admin</a>

    To open up an Edit screen for a specific record, I have a page called getEdit.php and I pass the id:

    <a href="javascript:openDiv(’Edit’,1,’edit’);" title="Edit Record 1">Edit</a>

  5. If I want to submit record 1 and save it, I can simply build a form that passes the action as ’save’. My response page then knows to save the record that I just edited.

I hope this makes sense! I use a similar approach with Payraise Calculator. I load the results of the page from getPayraise.php… you can even see the page here where I pull the content from. Nice and simple! As well, if I wanted, I could expand this out and use a widget or third party site to interact with it.

RSS feed | Trackback URI

9 Comments »

Comment by no imageSterling Camden (SezWho)
2006-11-24 20:31:18

Nice example. This is pretty typical of how a lot of Rails applications use AJAX. In these cases, X could stand for XHTML fragment.
Rate this:
2.9
 
Comment by no imageRoyans (SezWho)
2006-11-25 04:07:49

Look out for JSON as well which is another way of exchanging information without really using XML or XMLHttpRequest.
Rate this:
1.6
 
Comment by no imageRob (SezWho)
2006-11-25 10:19:49

I use a similar method for http://www.gameboar.com.

Raw HTML is pulled from the server using the xmlhttp. Then a dynamic string replace on the downloaded HTML code is performed to tailor the HTML to the user’s browser before showing the modified HTML on the page.

It’s the best (quickest and easiest) way I could figure out how to support multiple browsers with just one data stream format on the server.

I hope someone finds the info useful and check out the site to see it in action. ;-)

Rate this:
1.6
 
Comment by no imageAde (SezWho)
2006-11-25 12:39:43

I don’t think Ajax without the X is a bad idea. XML is great in a lot of instances, but I don’t think programmers should always use XML to interchange data just because it’s “the proper thing to do”.

Taking out the X in the instance you described makes the entire process much quicker, and takes much of the processing from the client side to the server side (which is a great thing in this age of Ajax apps that ramp my CPU to 100% utilization). And the fact that it’s easier to code means it’s probably easier to maintain and debug.

Rate this:
2.9
 
Comment by no imageDouglas Karr (SezWho)
2006-11-27 15:52:56

Thanks for all your nice comments! I’m growing weary of the ‘code snobs’ out there one the web that have to bash everyone’s approach to doing things. I’m always open to improvement - especially since I’m not a professional developer (though I do develop professionally :).

I appreciate the tips!

Sterling - I never thought of it that way. I’ve played with Rails but should probably look into it deeper.

Rob - WOW! Now that’s an aggregator!

Ade - always a pro! Can’t wait to get started working with you.

Royans - I’m settling into JSON soon. I’m seeing it popup in a lot more APIs.

Thanks all!!!

Rate this:
2.9
 
Comment by no imageDouglas Karr (SezWho)
2006-12-20 12:40:50

I just read some comments on this post at http://www.dzone.com and one friendly (there weren’t too many friendly) commentors added this reference:

http://en.wikipedia.org/wiki/AHAH

Asynchronous HTML and HTTP. How about that!

Rate this:
2.9
 
Comment by no imagestartoy (SezWho)
2007-10-25 19:49:52

there is also a large and comprehensive database of 800+ ajax scripts available with over at

ajaxflakes’s ajax scripts compound

thought i should add it might be helpful to others…

http://scripts.ajaxflakes.com here

Rate this:
3.0
Comment by no imageDouglas Karr (SezWho)
2007-10-25 19:57:24

Awesome startoy! Thanks!
Rate this:
2.9
 
 
2007-12-31 21:21:10

[...] Ajax without the X: Asynchronous dynamic content with JavaScript: 1,639 Pageviews [...]
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

My Comment Policy: I moderate comments. Please be patient:

  • Spam will happily be destroyed.
  • Use your real name, not some keywords. Otherwise it will be destroyed.
  • Mean comments aren't necessary. If I don't post them I will reply personally to let you know why.
  • Lewd comments will be edited, I don't want my readers leaving because of offensive content.
Great debate, criticism and colorful commentary is always appreciated and approved!