Call (877) SITE-OPT (748-3678)

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( );
}
Copyright © 2002-2017 Website Optimization, LLC. All Rights Reserved - Free website speed test - Privacy Policy
Last modified: February 19, 2009.

Follow us on: Twitter, Google+, Facebook, Linked In