Joshua Macmaoni
Joshua Macmaoni

Reputation: 43

Retrieve a value in a xml file and use it in another function in Javascript

I would like to retrieve a value in a xml file and use this value in another function.

I use:

function xmlparser() {
    var xmlhttp, myvar;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "http://www.domain.net/xmlfile.xml", true);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            myvar = xmlhttp.responseXML.documentElement.getElementsByTagName("date")[0].textContent;
        }
    }
    xmlhttp.send();
}

How can I use myvar in another function ? Like

function test() {
    alert(myvar);
}

Thanks

Upvotes: 1

Views: 120

Answers (1)

zigdawgydawg
zigdawgydawg

Reputation: 2046

Like so:

function test(myvar) {
    alert(myvar);
}

function xmlparser()
{
    var xmlhttp, xml_build, xml_dashboard;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET", "http://www.domain.net/xmlfile.xml", true);
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            myvar=xmlhttp.responseXML.documentElement.getElementsByTagName("date")[0].textContent;
            test(myvar);
        }
    }
    xmlhttp.send();
}

Upvotes: 1

Related Questions