简体   繁体   English

Facebook API - 用户登录后运行 javascript

[英]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:我还想运行一些代码来拉动 email,这样说:

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.当我在对 FB.init() 的调用下添加它时,它在 window 加载后立即运行,这是可以理解的,但不是我想要的。

I want the code to run AFTER the user has clicked the Login With Facebook button, and then successfully logged in, etc.我希望代码在用户单击 Login With Facebook 按钮后运行,然后成功登录等。

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.有关您可以订阅的更多事件,请参阅http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

updated with full example更新了完整的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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