views:

38

answers:

2
xmlhttp.onreadystatechange=function() {

So this says onreadystatechange, invoke function(). Can I put parameters in function()? Second question, what does it mean when someone writes, xmlhttp.onreadystatechange=statechanged? Does that mean it will always be true or something?

+2  A: 
  1. You can't use a parameter since onreadystatechange has no parameters to give you. What parameter are you expecting? It is simply a hook for handling the response. What you do have available is the xmlhttp.readyState which tells you if the response is ready or not, xmlhttp.status - the http status code (i.e. 200), and xmlhttp.responseText - the response itself.

  2. No - that means you are assigning a variable reference (a function) to onreadystatechange.

I would strongly suggest using a JS framework (such as jQuery) to do AJAX calls - it will abstract away the low-level details you are asking about. If you must use native JS AJAX calls - read this tutorial.

Yuval A
I just want to learn AJAX first before getting into jQuery.
Doug
A: 

You can pass parameters with a wrapper function:

var func = function(p1, p2) {/*...*/};
xmlhttp.onreadystatechange = function() {
  func(foo, bar);
};
Tgr