3

我的网站上有这段代码。facebook 登录对话框在我的页面加载时显示,而不是在用户单击锚标记时显示。

<script>
  // Additional JS functions here
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'xxxxxxxxxxxxxx', // App ID
      channelUrl : '//192.168.1.127/test/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

    // Additional init code here
    FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
        // connected
      } else if (response.status === 'not_authorized') {
        // not_authorized
        login();
      } else {
        // not_logged_in
       login();
      }
    });
  };

  function login() {
    FB.login(function(response) {
      if (response.authResponse) {
        testAPI() ;
      } else {
        // cancelled
      }
    });
  }

  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Good to see you, ' + response.name + '.');
    });
  }

  // Load the SDK Asynchronously
  (function(d){
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {return;}
      js = d.createElement('script'); js.id = id; js.async = true;
      js.src = "//connect.facebook.net/en_US/all.js";
      ref.parentNode.insertBefore(js, ref);
  }(document));
</script>

我希望在FB.getLoginStatus用户单击此事件时触发该事件:

<a href = "#" onClick = "FB.getLoginStatus()">Login</a>

但是现在它正在加载而我没有做任何事情。

建议?

4

1 回答 1

8

好的,这里有一个简单的解决方案 - 将调用包装FB.getLoginStatus在另一个函数中并让您的按钮调用函数。

function doLogin(){
  FB.getLoginStatus(function(response) {
    ...
  };
}

然后您可以将onClick按钮上的 更改为doLogin()

基本上,这里发生的情况是,一旦加载 Facebook SDK,它就会调用window.fbAsyncInit包含您的FB.getLoginStatus代码的函数,因此一旦 SDK 准备好,登录代码就会被执行。

于 2012-11-22T08:01:46.900 回答