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.
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:
- I have a div id=”dialogue” on my page and the style set to hidden:
<div id="dialogue" style="visibility:hidden"></div>
- 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;
} - 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);
} - 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>
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.

Douglas Karr

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.
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.
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!!!
http://en.wikipedia.org/wiki/AHAH
Asynchronous HTML and HTTP. How about that!
ajaxflakes’s ajax scripts compound
thought i should add it might be helpful to others…
http://scripts.ajaxflakes.com here