I'm trying to get the URL (str variable) using FB API.
When I tried CONSOLE2 console.log(str)
it showed the exact value of str variable which contains URL for photo.
But when I tried CONSOLE1 it didn't work. It printed nothing.
Is it because of some async functions? If yes, how can write the code that following two statements (see Note 1) work (it doesn't matter one executes after another, I just want the URL in CONSOLE1 where I have called getPhoto()
)
Note 1: Both statements mean the location.href and CONSOLE1, to ensure that I'm getting the value of str inside the Login()
function.
function Login()
{
FB.login(function(response) {
if (response.authResponse)
{
getPhoto();
// window.location.href="quiz.html";
//**CONSOLE1**
console.log(str);
} else
{
console.log('User cancelled login or did not fully authorize.');
}
},{scope: 'email,user_photos,user_videos,publish_actions'});
}
function getPhoto()
{
FB.api('/me/picture?type=normal', function(response) {
var str="<br/><b>Pic</b> : <img src='"+response.data.url+"'/>";
document.getElementById("status").innerHTML+=str;
//**CONSOLE2**
console.log(str);
});
}