2011-05-31 67 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

相關問題