home >
Quick easy add content to page with Ajax
Call the Perl/PHP etc... file to get your data. Return small XML in standard
format and innerHTML it to the page with the given ID. Ok so some of your content is now
in your data layer but it is easier than parsing the XML and creating the XHTML
from javascript... and you don't have the overhead of parsing some huge XML file,
which can be slow in javascript. Also look how short the code is below. Uses Rob's tmaj.js
for AJAX. Super easy.
add content
show xml
new content to go here
<script type="text/javascript">
function getStuff()
{
var req = ajaxQuery('get_stuff.php?id=blah', function () { processAjaxReturn(n, response, req) }, 'POST');
}
function processAjaxReturn(n, response, req)
{
if (!checkXML) return;
var ajaxXHTML = response.getElementsByTagName('ajax_xhtml')[0];
var nId = ajaxXHTML.getAttribute('id');
var sXHTML = ajaxXHTML.firstChild.data;
document.getElementById(nId).innerHTML = 'Request ' + req + "\n" + sXHTML;
}
/**
* Standard error checking on XML response
*/
function checkXML(response)
{
bReturn = true;
if (response.nodeName != 'ajax_response')
{
alert('no ajax_response node');
bReturn = false;
}
return bReturn;
}
</script>