jQuery function return value
I am very new to this, I made this function:
Code:
function getID(user){
$.getJSON('https://twitter.com/users/'+user+'.json?callback=?',
function(data)
{
$('#userid').html(data.id_str);
});
}
Works well, but I want that instead of modifying the value of #userid I return a value, I tried this:
Code:
function getID(user){
$.getJSON('https://twitter.com/users/'+user+'.json?callback=?',
function(data)
{
return data.id_str;
});
}
But it makes me return to the function within which it is, not getID.
Solutions?
Thanks!
Re: jQuery function return value
getJSON() is an asynchronous method, which I don't think can be used in that way. But getJSON() is essentially just a wrapper for jQuery's more generic ajax() method, which allows you to set its async property (it notes that JSONP cannot support synchronous operation, but it doesn't look like you need that). From there, you could get the jqXHR object that is returned by ajax() and parse that for your result.
Re: jQuery function return value
Now - I'm curious - why are you calling a twitter service like that?
Would they want to know you are doing that??
Re: jQuery function return value
Quote:
getJSON() is an asynchronous method, which I don't think can be used in that way. But getJSON() is essentially just a wrapper for jQuery's more generic ajax() method, which allows you to set its async property (it notes that JSONP cannot support synchronous operation, but it doesn't look like you need that). From there, you could get the jqXHR object that is returned by ajax() and parse that for your result.
Ok, thanks, I'll investigate. :)
Quote:
Now - I'm curious - why are you calling a twitter service like that?
Would they want to know you are doing that??
I just get the id of a twitter user. :)
Thanks! :P
Re: jQuery function return value
You might be interested in the Twitter API.