1

My code is as follows:

jQuery(document).ready(function() {
        var pic = 0;
        FB.init({appId: '1355701231239404', status: true, cookie: true, xfbml: true});
        FB.getLoginStatus(function(response) {
        if (response.session) {
            pic = "http://graph.facebook.com/" + response.session.uid + "/picture";     
            alert(pic);
         } else {
            window.location = "index.php"
         }       
        });

    });
    </script>

The issue is that the pic inside my if statement has a different scope with the one I declared above? Why and how do I make them the same?

What I want is to use this var pic (the one that has been assigned by the if statement) inside the body of my html. I will have another script inside the body, a document.write that uses this pic variable.. as of now when I do a document.write it always gives me 0, as if it's not assigned inside the if statement

2
  • 1
    @user721937 I know that you've found stackoverflow interesting because you've asked several questions in the last 24hrs. Welcome to SO! It's important to us that you review all your questions and accept the answers that solved them. Thank you. Commented Apr 24, 2011 at 18:07
  • willdo karlphillip don't worry Commented Apr 24, 2011 at 18:10

2 Answers 2

3

The issue is that the pic inside my if statement has a different scope

No, it isn't. JavaScript scope is by function. var pic = 0; scopes pic to the anonymous function passed to ready(). The anonymous function passed to getLoginStatus() doesn't use var at all, so the scope of pic there is the same.

(Since you didn't say what outcome you got or what outcome you were expecting, it is hard to say what the solution to your problem actually is)

Sign up to request clarification or add additional context in comments.

2 Comments

I will be using the pic variable inside the body of my html..so how do I get this? as of now I always get pic as 0, while what I want pic to have some value assigned in the if statement
The variable is scoped the the function you pass to ready and isn't available outside it. You could get around that using a global (which is ugly) but you would still have a timing issue. You send off a request to Facebook for some data, but it hasn't been processed before the code you put in the body (which you should have discussed in the original question) runs. You need to do all logic (that depends on the Facebook data) the in the callback function you pass to getLoginStatus()
1

But there is only one pic in that snippet. So the issue you describe doesn't exist. What problem are you experiencing? How does the code misbehave compared with what you expect?

Update

Okay, your actual problem is that you're expecting pic to be updated so you can use the value of it elsewhere. But consider:

FB.getLoginStatus(function(response) { ... });

The reason that function executes a callback, instead of returning response as a return value, is because it is asynchronous. It make take a while to get the response, and so it doesn't execute the callback immediately. So your other piece of code is probably executing before that happens, i.e. before pic has been properly initialised with a value.

1 Comment

@user721937 - no, never. The point of the callback is to provide a location to take actions that depend on the result of the previous call - just put the code that needs pic in the callback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.