Facebook API-在用户登录后运行javascript

Facebook API - Run javascript after user has logged in

本文关键字:运行 javascript 登录 用户 API- Facebook      更新时间:2023-09-26

现在我有这个:

<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>    

我也想运行一些代码来提取电子邮件,比如说:

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

当我在对FB.int()的调用下添加它时,它在加载窗口后立即运行,这是可以理解的,但不是我想要的。

我希望代码在用户点击"使用Facebook登录"按钮后运行,然后成功登录,等等。

有人能告诉我正确的方向吗?我在谷歌上搜索过,但似乎什么都找不到:/

事先谢谢你,汤姆。

您可以使用以下事件:

<!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>

参见http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/了解更多您可以订阅的活动。

使用完整示例进行更新