2011-05-31 31 views
1

在一分钟我有这样的:Facebook的API - 用户后运行JavaScript已经登录

<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.init()时添加了这个,它在窗口加载后立即运行,这是可以理解的,但不是我想要的。

我想给用户之后运行代码点击了登录与Facebook按钮,然后成功登录等

任何人都可以点我在正确的方向,我搜索谷歌,但可以” t似乎找不到任何东西:/

在此先感谢,汤姆。

回答

3

您可以使用事件这样的:

<!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/你可以订阅更多的事件。

全例如

+0

嗨更新,我尝试这样做,但是当我添加了调用FB.api()块内,它仍然运行在页面加载时。我的完整代码是:http://pastebin.com/2rr9Q94d谢谢。 – Tom 2011-05-31 11:11:13

+0

试试这个方法http://pastebin.com/1K0Cur0E – Rufinus 2011-05-31 11:15:20

+0

谢谢,我试过这种方式,当窗口加载时它会停止运行代码,但它在点击按钮时不响应(警报窗口不会响应不会弹出):S – Tom 2011-05-31 11:21:54

相关问题