< Back to Code Samples
// De-serialize the words.
// Looks like: NOUN_1:Bag,NOUN_2:Bucket,ADJECTIVE_1:Brown
//
function parseResponse(text)
{
var output = text.split(',');
for (i = 0; i < output.length; i++)
{
item = output[i].split(':'); // parse off the individual item
document.getElementById(item[0]).innerHTML = item[1];
}
}
// Fill out the result. Like I said before, this is before I knew how to chain AJAX calls.
// First, it runs through all the links in the document, and any links that have the class "word"
// are appended to the request. The "id" is the ordinal, and the The "rel" is the part of speech.
//
// When it's done with all of those, it sends the request, and then parses the response on return.
//
function fillout()
{
var url = 'madlibs.php?mode=3';
if (document.links)
{
for (i = 0; i < document.links.length; i++)
{
if (document.links[i].className == 'word')
{
url += '&' + document.links[i].id + '=' + document.links[i].rel;
}
}
xmlhttp.open("GET",url);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
parseResponse(xmlhttp.responseText);
}
}
xmlhttp.send(null);
}
}