www.webdeveloper.com
Results 1 to 6 of 6

Thread: Get variable from another method

  1. #1
    Join Date
    Mar 2008
    Posts
    32

    Get variable from another method

    Hi I'm trying to access a variable that is the result of a HTTP request function in another method. I'm trying to return the value str though, and can't seem to get it. All I am returned with is 'undefined' instead of the value of str.

    Strangely this works in an alert, but it won't seem to work as a var. Does anyone know how to reference it?

    Thanks

    Code:
    var a= document.getElementById("lfmsearch");
    var b= document.getElementById("lfmusearch");
    
    var returntags = tracktags(a,b);
    The method I want the value str from:
    Code:
    function tracktags(string, string2)
    {
    
    var str = "";
    var http = new XMLHttpRequest(); //new request
    
    http.open("GET", "http://ws.audioscrobbler.com/1.0/track/"+string+"/"+string2+"/toptags.xml", true);//post request
    
    http.onreadystatechange = function() {//Call a function when the state changes.
    	if(http.readyState == 4 && http.status == 200) {
    
    
    var p= http.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
    var p2= http.responseXML.getElementsByTagName("name")[1].firstChild.nodeValue;
    var p3= http.responseXML.getElementsByTagName("name")[2].firstChild.nodeValue;
    var p4= http.responseXML.getElementsByTagName("name")[3].firstChild.nodeValue;
    var p5= http.responseXML.getElementsByTagName("name")[4].firstChild.nodeValue;
    
    
    var f = str + " " + p + " " + p2 + " " + p3 + " " + p4 + " " + p5;
    str = f;
    
    
    return str
    
    }
    
    }

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    As far as I can see, str is returned by the http.onreadystatechange, and not by the function you are calling. I'd say that if you alert returntags, you will get undefined?

  3. #3
    Join Date
    Mar 2008
    Posts
    32
    Hi, thanks for the reply, basically I'm having the same problem whatever I do, I can't get a reference to the variable outside of the function. I need the function there to actually obtain the data, but also need to get the result variable out of it for use in other methods. Does anyone know how I can do this? If I try and reference it outside the function I'm returned with nothing.

    Code:
    function tracktags(string, string2)
    {
    var x = document.getElementById("temp");
    var str = "";
    var http = new XMLHttpRequest(); //new request
    
    http.open("GET", "http://ws.audioscrobbler.com/1.0/track/"+string+"/"+string2+"/toptags.xml", true);//post request
    
    http.onreadystatechange = function() {//Call a function when the state changes.
    	if(http.readyState == 4 && http.status == 200) {
    
    
    var p= http.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
    var p2= http.responseXML.getElementsByTagName("name")[1].firstChild.nodeValue;
    var p3= http.responseXML.getElementsByTagName("name")[2].firstChild.nodeValue;
    var p4= http.responseXML.getElementsByTagName("name")[3].firstChild.nodeValue;
    var p5= http.responseXML.getElementsByTagName("name")[4].firstChild.nodeValue;
    
    
    var f = str + " " + p + " " + p2 + " " + p3 + " " + p4 + " " + p5;
    
    
    
    }
    
    
    }
    str = f;
    
    x.setAttribute("value", str);
    alert(x.value);
    	
    
    http.overrideMimeType('text/xml');
    
    http.send(null);// send request

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    var str;//global
    function tracktags(string, string2)
    {
    var x = document.getElementById("temp");
    str = "";
    ...

  5. #5
    Join Date
    Mar 2008
    Posts
    32
    Thanks, I tried using a global variable but I still can't get access to it elsewhere outside of the function. What I'm attempting here in the bigger picture is the following.

    A function in another method iterates through a series of music tracks associated with an artist, and for each track, I'm using the above method to get the tags associated with it.

    So in theory for each track, this is the order of events I'm looking for:

    For each track...
    - Method getInfo() gets the artist name and song name
    - Run the method tracktags(a,b) with these names.
    - tracktags method sets the value of a hidden form element to the value of the tags returned.
    - getInfo() method then takes the value of the form element (and for each track, this should be the tags returned for it).

    Problem is, tracktags only sets the form element value when all tracks have been iterated through in the for loop of getInfo(). Can someone point out where I'm going wrong? Here are the two methods I'm using..



    Code:
    function getInfo(event)
    {
    var c = document.getElementById("temp");//hidden form element
    var c2 = c.value;
    
    
    for (var i = 0; i < track.length; i++) {
    
    var p // track name
    var q // artist name
    var returntags = tracktags(q,p);
    alert(q + p + c2);
    }}
    Code:
    function tracktags(string, string2)
    {
    var x = document.getElementById("temp");
    var str = "";
    var t = new XMLHttpRequest(); //new request
    
    t.open("GET", "http://ws.audioscrobbler.com/1.0/track/"+string+"/"+string2+"/toptags.xml", true);//post request
    t.onreadystatechange = function() {//Call a function when the state changes.
    	if(t.readyState == 4 && t.status == 200) {
    
    
    var p= t.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
    var p2= t.responseXML.getElementsByTagName("name")[1].firstChild.nodeValue;
    var p3= t.responseXML.getElementsByTagName("name")[2].firstChild.nodeValue;
    var p4= t.responseXML.getElementsByTagName("name")[3].firstChild.nodeValue;
    var p5= t.responseXML.getElementsByTagName("name")[4].firstChild.nodeValue;
    
    
    var f = str + " " + p + " " + p2 + " " + p3 + " " + p4 + " " + p5;
    
    str = f;
    
    x.setAttribute("value", str);// Set value of hidden form element
    
    }
    
    
    }
    
    
    t.overrideMimeType('text/xml');
    
    t.send(null);// send request
    
    
    }

  6. #6
    Join Date
    Mar 2008
    Posts
    32
    I'm now using a global variable for str with this method, but it still only updates after all items in the for loop have been iterated through, can someone explain to me why this is happening?

    From what I've attempted I can't see why it doesn't just take the tag data for each track item, and then reset the value for each item that is iterated.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles