TechWelkin

Tips and Tutorials on Computers, Mobiles and Internet

Show navigation Hide navigation
  • iPhone / iPad
  • Make Money Online
  • MS-Excel
  • MS-Word
  • AdSense
  • About Lalit
  • Contact us
  • Tools
    • Hindi Proof Reader
    • Top Tech Blogs in India
    • QR Code Generator
TechWelkin Site Logo
Last updated: 16 October 2014

AJAX: Passing Parameters To onreadystatechange Function

November 27, 2008 By Lalit Kumar 9 Comments

In AJAX paradigm, onreadystatechange function of xmlHttp object does not accept any parameters. Learn how you can make this function do what you want.

When the response of an AJAX request is ready on the server, value of the readyState property of XMLHttpRequest object is changed to “4”. This means that the request is now complete and response could be received. onreadystatechange property of the same object stores a user-defined anonymous function which is executed whenever readyState property changes. A typical piece of code for this would be as below:

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 parameters to it. So, if you want to pass parameters to the anonymous function -you can do it as below:

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

function stateChanged(p1, p2)
{
        if(xmlHttp.readyState==4)
        {
            //do something with the response
        }
}

Isn’t it pretty clever solution?! It is like taking a bit long route -but this route is not all that difficult -is it? Hope this helps you and save you time. Let me know how you liked this tip. Thank you for using TechWelkin.

Related Articles for More Information:

  1. Java Memory Management: Heap Space and Garbage Collector
Last updated: 16 October 2014 | Published on: 27 November 2008 | Authored and Edited by: Lalit Kumar and Team | Filed as:

Filed Under: Tips and Tricks

Lalit Kumar is the Principal Author and Founder of TechWelkin. He is a web explorer and he enjoys finding useful information on the Internet. He loves to put things together to create a bigger solution. Lalit is passionate about technology, languages and literature. You can contact Lalit via email (techwelkin [at] gmail [dot] com) or Facebook (facebook.com/techwelkin).

Comments

  1. William Jones says

    August 27, 2017 at 8:28 am

    Thanks this help me a lot. Ran into one hick-up. Down inside some nest statements is where I was calling this. Values declared outside the last statement, ended up being empty strings in side the callback. Simple fix, just set the parameter to n new variable:

    var parameter1 = “Something”;
    for (var i =0; i < 100; i++) {
    var _parameter1 = parameter1;
    xmlHttp.onreadystatechange=function () {
    stateChanged(_parameter1, parameter2);
    };
    xmlHttp.open("GET", handlingURL, true);
    xmlHttp.send(null);
    }
    }

    Reply
  2. Rohit says

    December 10, 2013 at 4:52 pm

    Really Helpful code Man
    Thanks A Lot

    Reply
  3. lucas says

    May 7, 2013 at 5:18 am

    thank

    Reply
  4. Sailendu says

    May 3, 2013 at 4:52 pm

    Thanks you so much, it really worked. :)

    Reply
  5. theone says

    November 23, 2012 at 6:22 pm

    thank you dear .. you are great

    Reply
  6. theone says

    November 23, 2012 at 6:22 pm

    thank you dear .. you are great

    Reply
  7. Englishman says

    June 18, 2009 at 2:02 pm

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

    Reply
  8. Ercan says

    May 17, 2009 at 1:06 pm

    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 …

    Reply
  9. thiyagarajan.G says

    January 2, 2009 at 5:06 am

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

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Stories

  • Download Google Maps Offline Navigation on Android or iPhone
  • iPhone Charging Problem: Reasons and Solutions
  • GST Guide for Bloggers, AdSense Publishers and Freelancers
  • iOS: Display Accommodations, Inverse Colors and Color Filtering
  • How to Increase the Transparency of Start Menu & Taskbar in Windows 10
  • Link Aadhaar Card with Mobile Number (Vodafone, Jio, Airtel, BSNL, Idea)
  • How to Delete Unnecessary Apps from Your iPhone or iPad
  • How to Create and Use iPhone Medical ID for Emergency Information

Tools

  • Hindi Proof Reader
  • Top Tech Blogs in India
  • QR Code Generator
  • Random Word Generator
  • Strong Password Generator
  • Trace Mobile Numbers

Connect with us

  • Facebook
  • Google+
  • Twitter
  • LinkedIn
  • Pinterest
  • RSS Feed

© 2007-2018 TechWelkin.
The content is copyrighted to Lalit Kumar and may not be reproduced on other websites.
TechWelkin displays Google AdSense ads. See how Google uses data when you're on TechWelkin | Privacy Policy