1

wondered whether anyone can help answer my question, I need to get the xml content from "data", as it is defined within a nested function within the yam_send function, I am trying to use return to get the variable back to the function 'GetBasicStatus' but it doesn't get all the way, can anyone shed any light on this. Sorry if this is an obvious solution, bit of a beginner I'm afraid.

function GetBasicStatus()

    {
        //do some stuff & define variable "command"
        data = yam_send(command);

        if (window.DOMParser)
        {
            parser=new DOMParser();
            xmlDoc=parser.parseFromString(data,"text/xml");
        }
        else    // Internet Explorer
        {
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async=false;
            xmlDoc.loadXML(data);
        }
    }

    function yam_send(str)
    {

        var xmlhttp;

        if (window.XMLHttpRequest)

        {// code for IE7+, Firefox, Chrome, Opera, Safari

        xmlhttp=new XMLHttpRequest();

        }

    else

        {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }


    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var data=xmlhttp.responseText;
            return data;
        }


    }
    xmlhttp.open("GET","test.php?str",true);
    xmlhttp.send();
    }
1
  • Thanks for the heads up Jon, that explains alot! :-) Commented Nov 5, 2012 at 19:43

1 Answer 1

1

Function you'd provided as xmlhttp.onreadystatechange is a callback. It's executed when event is happened and can't return anything to "parent" function.

You can create some variable in a parent function and assign some value to it in callback. Or use parent funcation as a callback, like this:

var xmlResponse = null; //variable for usage outside of GetBasicStatus
function GetBasicStatus(data) {
    xmlResponse = data; //now you can use it.
    if (window.DOMParser)
    {
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(data,"text/xml");
    }
    else    // Internet Explorer
    {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async=false;
        xmlDoc.loadXML(data);
    }
}

function yam_send(str)
{
    var xmlhttp;

    if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                var data=xmlhttp.responseText;
                GetBasicStatus(data);
            }
        }
    xmlhttp.open("GET","test.php?str",true);
    xmlhttp.send();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Zlebnik Thanks for your comment; its the xmlhttp.responseText I need, how can I make use of this outside of the callback, I can use the this to assign new values to HTML elements but I need to be able to process the result in another function, I have a feeling I am just appoaching the problem from the wrong angle?

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.