Early this year I was talking to a friend of mine and he told me that he was working on an open source project called xajax. At the time I hadn’t really started to play with Ajax yet but the project sounded interesting so I filed it away in the back of my mind for future retrieval. Then in May my company sent me to The Ajax Experience in San Fransisco. That got me all excited about what could be done with Ajax. It also scared me because of the dificulties that people experience when using Ajax. So now one of my projects has got me using xajax.
As far as tools go, xajax seems to be a good one so far. It has many of the things that I want (unabtrusive, lightweight, extensible) without being overbearing.
So far I have stuck to a basic - edit table rows in place - implementation and even that has given me headaches. What I find is that most of the headaches are related to the poor error logging in the browsers or the incompatibility of the browsers. I started by using a link to call the javascript function like this <a href=”javascript:xajax_myFunction()”>click here</a>. That worked fine in IE but was giving me problems in Firefox. I chose to use a button instead of tracking down the problem (this is only a prototype) and got that working in Firefox but could not get it to work in IE. It turns out that you can set innerHTML in a <tr> tag in Firefox, but not in IE. So I moved things around and started setting the innerHTML in the <td> tags instead and then it stopped working in Firefox but started working in IE. Eventually I found that I had to move my <button> outside of the form in order to get it to work in Firefox. This whole time I am working with a new tool - xajax - and wondering if it is the tool or the browser. Well, I decided it was the browser and the tool worked as advertized.
If you are thinking about using Ajax in one of your projects, remember that it takes a lot of patience. If your project is in PHP then take a look at xajax. It doesn’t do everything Ajax for you, but it give a nice abstraction layer for the usual stuff.

Thanks for the review Russ. Dealing with browser differences truly is the biggest headache when dealing with ajax and HTML DOM manipulation using javascript. Little by little, however, one learns little techniques that work in many, if not all, browsers.
Invoking xajax functions through a link tag is best handled like this:
<a href=”xajax_myFunction(); return false;” onclick=”xajax” >click me</a>
According to standards, returning false from an event tells the browser to cancel the default action, which in this case is to follow the link instead of waiting for the xajax call to return. Standards also do not technically allow “javascript:” in the href attribute.
Returning false from the event works for Forms as well:
<form id=”myForm” onsubmit=”xajax_myFunction (xajax.getFormValues (xajax.$(’myForm’))); return false;” >
<input type=”text” id=”myText” name=”myText” />
<input type=”submit” value=”click me” />
</form>
When debugging, keep in mind that even if you have a “return false;” if any of the javascript before the “return false;” breaks, the javascript parser will often stop before it reaches the return and the default action will be executed (i.e. submitting the form or following the link) anyway!
Another little thing that confuses a lot of people is that <tr> tags inserted using javascript have to be added to a tbody tag and not directly to a table tag if you want it to work in both IE and Firefox.
On a bright note, hopefully the new plugin API in the upcoming version of xajax (0.5) will allow community members to create useful plugins for xajax that will abstract a lot of this kind of esoteric knowledge needed to manipulate the DOM using javascript into easy-to-use plugins. It will also allow users to create plugins that xajax can use to incorporate third party libraries like scriptaculous and moofx that are designed to simplify the kinds of DOM manipulations that developers typically want to make. That way we can keep xajax itself lightwight, but allow people to optionally extend it in ways that others can be easily distributed and shared. We have a beta of xajax 0.5 out right now which is available from the community forum or you can check out the latest from the trunk of the sourceforge SVN repository.
The tests folder that comes packaged with xajax is an under-utilized feature. It is full of short, useful (hopefully working
) example code for most of the functions offered by xajax.
Next time you run into a problem with xajax and javascript DOM manipulation, feel free to IM me before you waste too much time on something that I may be able to help you solve in a few minutes.
whoops…I guess those less than and greater than entities confused me:
code above should read:
<a href=”" onclick=”xajax_myFunction(); return false;” >click me</a>
Thanks J.
It seems to me that in some cases it would be better to manipulate the DOM instead of using innerHTML. Plug-ins that will handle those kind of details could be really useful.
In my current project, I am doing inline updates to a database table. There are a couple of design decisions that I had to make:
In the end I decided to do a replacement of the table columns to go from viewing the data row to editing the data row to reduce the initial page load size. I then decided to a separate form element for each column because I wanted the form elements to appear under the correct table header and I was able to use xajax.getFormValues() along with array_merge() to pull all the entered data together very cleanly.
Hi All Experts,
I want to use AJAX (Asynchronous JAVA script with XML ). How can i Optimize the site SEO.
as Java script and flash is not recommended by search engines. Any suggestion or help is welcomed. With Regards.
I have problem using href in xajax. My code is like this
addAssign(”div1″,”innerHTML”,”This is Admin”);
return $objResponse;
}
function showJSeekers()
{
$objResponse = new xajaxResponse();
$objResponse -> addAssign(”div1″,”innerHTML”,”This is JSeekers”);
return $objResponse;
}
// Instantiate the xajax object. No parameters defaults requestURI to this page, method to POST, and debug to off
$xajax = new xajax();
//$xajax->debugOn(); // Uncomment this line to turn debugging on
// Specify the PHP functions to wrap. The JavaScript wrappers will be named xajax_functionname
$xajax->registerFunction(”showAdmin”);
$xajax->registerFunction(”showJSeekers”);
// Process any requests. Because our requestURI is the same as our html page,
// this must be called before any headers or HTML output have been sent
$xajax->processRequests();
?>
jBurst Admin
printJavascript(’../’); // output the xajax javascript. This must be called between the head tags ?>
Admin
JSeekers
JSeekers
JSeekers
The button that calls xajax_showJSeekers() just works fine and neither of the objects. To try the page please check
http://www.jburst.com/ajax/xajax/examples/jburst_admin.php
It works neither in IE nor FireFox.
Thanks,
Reza
Reza,
If I am reading this correctly, the problem is the url that you are using in the href is not correct. As J. Max mentioned above, you should move the call to xajax_showJSeekers into the onclick attribute of the <a…> tag. more like this <a href=”" onclick=”xajax_showJSeekers();return false;”>. I hope this helps.
That was great. Now it works. Thanks Russ. I appreciate that
Is there a way to load a long list of records partially say into a control like and avoid NEXT PAGE/PREV PAGE kind of record navigation? In the new Yahoo Email they do the same but I don’t know how they have implemented that. Say in the new Yahoo Email all your emails will show on the same list and when you scroll down the list it loads the messages. This way there is no longer Next/Prev page.
Yes, there is. The concept is not too hard, though I’ve not done it so I can’t say how hard the execution is. My first thought on implementing something like this would be to have a div with scroll bar. As the user scrolls down you can call the xajaxResponse->addAppend() to add new items to the bottom of the list. The key is to know how many items to add to the list as the scroll bar moves.
I have problem setting the page title in xajax. I use the following code but it changes nothing
addAssign(”pg_title”,”innerText”,”New Title”);
return $objResponse;
} // showPageTitle
// Instantiate the xajax object. No parameters defaults requestURI to this page, method to POST, and debug to off
$xajax = new xajax();
$xajax -> registerFunction(”showPageTitle”);
$xajax -> processRequests();
?>
Welcome
printJavascript(”../”) ; // output the xajax javascript. This must be called between the head tags
?>
function doBodyOnLoad()
{
xajax_showPageTitle() ;
}
I’m guessing that the code didn’t show up correctly in your comment Reza. Is the function doBodyOnLoad supposed to be a php or javascript function. I’m just not sure what I am missing. Will you provide more information?
Sorry I made a mistake in copy and paste. Anyway I found the answer you should simply use the this code:
document.title = ‘New Title’ ;
instead of the standard
document.getElementById(’title_id).innerText = ‘New Title’
Thanks,
Reza
I have set this code to change the default error handler
// Set error handling and logging functions
error_reporting(0);
$old_error_handler = set_error_handler(”jErrorHandler”);
I get this error:
Undefined index: query in the file xajax.inc.php on line 938