niche Posted September 1, 2011 Share Posted September 1, 2011 I'm looking at a script at: http://w3schools.com/php/php_ajax_php.aspunder: Example Explained - The HTML Page:regarding:xmlhttp.onreadystatechange=function()I know xmlhttp is a variable and onreadystatechange is storing the function function(), but where is the function function() defined? Link to comment Share on other sites More sharing options...
jeffman Posted September 1, 2011 Share Posted September 1, 2011 xmlhttp.onreadystatechange=function() { // function body starts here if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } // function body ends here Everything in the code block following function() is the function. This is an example of an anonymous function, one of JavaScript's better features. You could also assign a named function to onreadystatechange. Link to comment Share on other sites More sharing options...
ShadowMage Posted September 1, 2011 Share Posted September 1, 2011 That is what is called an anonymous function (also, lambda function). It is a function without a name, in essence. It is defined in the same place it is assigned: //This first line is the assignmentxmlhttp.onreadystatechange=function()//The rest of the code defines the function{if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; }} EDIT: Shucks, too slow! You could also assign a named function to onreadystatechange.That would look something like this:function sendRequest() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; }}xmlhttp.onreadystatechange=sendRequest; Link to comment Share on other sites More sharing options...
niche Posted September 1, 2011 Author Share Posted September 1, 2011 Thanks for your help Deirdre's Dad & ShadowMage. You nailed what would've been my next question ShadowMage. How zen of you.Thanks Again,Niche Link to comment Share on other sites More sharing options...
ShadowMage Posted September 1, 2011 Share Posted September 1, 2011 You nailed what would've been my next question ShadowMage. How zen of you. (Even though my function was misleadingly named. A more appropriate name would have been processRequest ) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.