/    Sign up×
Community /Pin to ProfileBookmark

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);

[/code]

The method I want the value [b]str[/b] 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

}

}
[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Apr 16.2008 — 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?
Copy linkTweet thisAlerts:
@Hysteria86authorApr 17.2008 — 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.

<i>
</i>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 &amp;&amp; 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



Copy linkTweet thisAlerts:
@KorApr 17.2008 — <i>
</i>[COLOR="Blue"]var str;[/COLOR][COLOR="YellowGreen"]//global[/COLOR]
function tracktags(string, string2)
{
var x = document.getElementById("temp");
[COLOR="Blue"]str = "";[/COLOR]
...
Copy linkTweet thisAlerts:
@Hysteria86authorApr 17.2008 — 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..



<i>
</i>
function getInfo(event)
{
var c = document.getElementById("temp");//hidden form element
var c2 = c.value;


for (var i = 0; i &lt; track.length; i++) {

var p // track name
var q // artist name
var returntags = tracktags(q,p);
alert(q + p + c2);
}}


<i>
</i>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 &amp;&amp; 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


}
Copy linkTweet thisAlerts:
@Hysteria86authorApr 18.2008 — 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.
×

Success!

Help @Hysteria86 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...