how can i this code to get user id in another functions and use as global variable??
function get_id() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and connected to your
// app, and response.authResponse supplies
// the user’s ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
//but not connected to the app
} else {
// the user isn't even logged in to Facebook.
}
});
}
fb_user_id = get_id();
Your best bet would be to set this variable inside the function instead of assignment through calling:
Then later on in your page (provided the call is not dependent on this one immediately), you can access your variable anywhere on the page as the global variable
fb_user_id(also known aswindow.fb_user_id.If you need to run code as soon as the user ID is ready, you will need to use a callback. If you are using jQuery 1.5 you can also use jQuery Deferreds to help with the synchronicity issues: