Returning a Value From the Bit.ly JS API Callback

Posted by mtorbin on Stack Overflow See other posts from Stack Overflow or by mtorbin
Published on 2010-04-12T14:17:53Z Indexed on 2010/04/12 14:23 UTC
Read the original article Hit count: 364

Filed under:
|
|
|
|

Hey all,

I am attempting to turn this "one shot" script into something more extensible. The problem is that I cannot figure out how to get the callback function to set a value outside of itself (please note that references to the Bit.ly API and the prototype.js frame work which are required have been left out due to login and apiKey information):

CURRENTLY WORKING CODE

 var setShortUrl = function(data) {
var resultOBJ, myURL;
for(x in data.results){resultOBJ = data.results[x];}
for(key in resultOBJ){if(key == "shortUrl"){myURL = resultOBJ[key];}}
alert(myURL);

} BitlyClient.shorten('http://www.thinkgeek.com', 'setShortUrl');

PROPOSED CHANGES

var setShortUrl = function(data) {
var resultOBJ, myURL;
for(x in data.results){resultOBJ = data.results[x];}
for(key in resultOBJ){if(key == "shortUrl"){myURL = resultOBJ[key];}}
alert(myURL);
return myURL;

} var myTEST = BitlyClient.shorten('http://www.thinkgeek.com', 'setShortUrl'); alert(myTEST);

As you can see this doesn't work the way I'm am expecting. If I could get a pointer in the right direction it would be most appreciated.

Thanks,

  • MT

© Stack Overflow or respective owner

Related posts about bit.ly

Related posts about prototype.js