Code: Inline localized functions

Chapter 8 - Ajax Optimization

In the original example, we associated a callback function with the onreadystatechange. Because it is so small, we may as well inline it as an anonymous function, like so:

if (xhr)
{
    xhr.open("GET","sayhello.php",true);
    xhr.onreadystatechange = function( ){if (xhr.readyState == 4 && xhr.status == 200)
    {

    var parsedResponse = xhr.responseXML;
    var responseString = parsedResponse.getElementsByTagName("message")[0].firstChild.nodeValue;
    var output = document.getElementById("output");
    output.innerHTML = responseString;
    }};

    xhr.send( );
}