Jump to content

tutorial explaination


niche

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...