简体   繁体   中英

Facebook API - Run javascript after user has logged in

at the minute I have this:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>    
<script>
  window.fbAsyncInit = function() {
    FB.init({ appId:'188492754501683', cookie:true, status:true, xfbml:true }); 
  };
</script> 
<fb:login-button perms="email">
  login with Facebook
</fb:login-button>    

I want to run some code to pull the email aswell, say this:

FB.api('/me', function(res) { if (res != null) { alert(res.email); } } );

When I added this under the call to FB.init(), it was run as soon as the window loaded, which is understandable but not what I wanted.

I want the code to run AFTER the user has clicked the Login With Facebook button, and then successfully logged in, etc.

Can anyone point me in the right direction, I've searched google but can't seem to find anything:/

Thanks in advance, Tom.

you can use the events for this:

<!DOCTYPE html>
<html  xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
    <title>Facebook Test</title>
</head>
<body>
        <div id="fb-root"></div>        
        <script>
window.fbAsyncInit = function() {
    FB.init({appId: 'YOUR_APP_ID', status: true, cookie: true, xfbml: true});
    FB.Event.subscribe('auth.login', function(response) {
        // user logedin
    });
};
        (function() {
            var e = document.createElement('script');
            e.type = 'text/javascript';
            e.src = document.location.protocol +
                '//connect.facebook.net/en_US/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
        }());

</script>

        <fb:login-button perms="email">
            Login with Facebook
        </fb:login-button>        
</body>
</html>

see http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/ for more events you could subscribe to.

updated with full example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM