0

I would like to pass as parameter a generic function to be the onreadystate function, how can i do that and acess the xmlhttpobj? Something like that:

    function xmlHttp(target, xml, readyfunc) {
        if (window.XMLHttpRequest) {
            httpObj = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            httpObj = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (httpObj) {
            httpObj.onreadystatechange = readyfunc;
            httpObj.open("POST", target, true);
            httpObj.send(xml);
        }
    }
    function Run (Place){
       if (xmlhttp.readyState==4 && xmlhttp.status==200)
            //do a lot of things in "Place"
   }
1
  • You're missing a var before the httpObj. Don't try to make it global. Commented Apr 9, 2014 at 11:43

2 Answers 2

1

The function will be called in the context of the XHR object that the readychange event fires on.

Use this inside the function to reference the object.

Sign up to request clarification or add additional context in comments.

Comments

0

All you should do is using this keyword or changing your code like this:

function Run (){
   if (this.readyState==4 && this.status==200){
        //do a lot of things in "Place"
   }
}

the other way is passing the xhr object as an argument:

httpObj.onreadystatechange = function(){
    readyfun(this);
};

then you should change the Run function like:

function Run(httpObj){
   if (httpObj.readyState==4 && httpObj.status==200){
        //do a lot of things in "Place"
   }
}

now you could call the xmlHttp function like this:

xmlHttp(target, xml, Run);

or

xmlHttp(target, xml, function(httpObj){
    Run(httpObj);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.