jquery - Pushing a variable outside of a function -
i have jquery code block twitter widget i'm developing ,
$.getjson("http://twitter.com/users/show.json?screen_name=" + twitterfeed + "&callback=?" , function(data) { var fstring = ($('<div id="userimage"><h1>'+ data.followers_count +'</h1></div>').digits()).text(); var tstring = ($('<div id="userimage"><h1>'+ data.statuses_count +'</h1></div>').digits()).text(); $('#left-sidebar').prepend('<div id="userimage"><h1>'+ tstring +'</h1></div>'); $('#left-sidebar').prepend('<div id="userimage"><h2>tweets</h2></div>'); $('#left-sidebar').prepend('<div id="userimage"><h1>'+ fstring +'</h1></div>'); $('#left-sidebar').prepend('<div id="userimage"><h2>followers</h2></div>'); $('#left-sidebar').prepend('<div id="userimage"><img id="profileimagelarge" src=' + data.profile_image_url + '></div>'); });
i want grab fstring
, tstring
, use them in outside functions.
i need value of data.followers_count
, data.statuses_count
passed along variable value correct in function instead of undefined.
you don't need put them in global context using window.fstring, can move var declarations outside of $.getjson.
in javascript closures allow access variables defined outside function, vars defined in function not accessible outside.
var fstring, tstring; $.getjson(... , function(){ fstring = ...; });
any functions defined in same scope s $.getjson have access fstring , tstring
Comments
Post a Comment