478 views
by Lalit Kumar  on November 27, 2008

When the response of an AJAX request is ready on the server the value of the readyState property of the XMLHttpRequest object is changed to “4″ which means the request is complete. The onreadystatechange property of the same object stores a user-defined anonymous function which is executed whenever the readyState is changed. A typical piece of code would be like:

xmlHttp.onreadystatechange=function () {
if(xmlHttp.readyState==4)
{
//do something with the response
}
};

However, sometimes it is required to pass one or more parameters to this anonymous function. It sounds tricky but it is pretty simple! This anonymous function can not take parameters but it can call another function defined in the same file and pass paramters to it. So, if you want to pass parameters to the anonymous function -you can do it as below:

xmlHttp.onreadystatechange=function () {
            stageChanged(parameter1, parameter2);
        };
    xmlHttp.open("GET", handlingURL, true);
    xmlHttp.send(null);
}

function stageChanged(p1, p2)
{
        if(xmlHttp.readyState==4)
        {
            //do something with the response
        }
}
GD Star Rating
loading...
AJAX: Passing parameters to onreadystatechange function, 5.0 out of 5 based on 1 rating
  • http://www.thiyagarajan.0fees.net thiyagarajan.G

    thank a lot, this coding very use full for me by thiyagarajan

  • Ercan

    Hello ,thanks your AJAX code.

    I spend to mauch time to solve this situation BUT …
    xmlHttp.onreadystatechange=haberD(bak); DOWS NOT WORK (FUCK OF 2 hour …:)
    :)))

    You realy helped me. THANKS.

    I found yor from google by typing:xmlHttp.onreadystatechange function

    THANKS FROM TURKEY …

  • http://www.englishelp.ru Englishman

    Great solution. That’s exactly i was searching for. Thanks!

  • theone

    thank you dear .. you are great

  • theone

    thank you dear .. you are great

  • Sailendu

    Thanks you so much, it really worked. :)

  • lucas

    thank