[英]Possible to find out whether a user is logged into facebook over javascript API?
[英]Facebook API - Run javascript after user has logged in
此刻我有這個:
<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>
我還想運行一些代碼來拉動 email,這樣說:
FB.api('/me', function(res) { if (res != null) { alert(res.email); } } );
當我在對 FB.init() 的調用下添加它時,它在 window 加載后立即運行,這是可以理解的,但不是我想要的。
我希望代碼在用戶單擊 Login With 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/ 。
更新了完整的例子
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.