0

我无法弄清楚如何成功加载和执行 fb.ui 函数。我看到您需要有一个 facebook 应用程序 ID,所以我创建了一个空白应用程序并将其放在 appId 部分。这是我当前的代码:

加载和初始化:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {

    FB.init({
      appId      : '1404387053111932', 
      channelUrl : 'channel.htm;', 
      status     : true, 
      xfbml      : true 
   });


};

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

FB.ui 脚本:

<script>
FB.ui(
  {
   method: 'feed',
   name: 'The Facebook SDK for Javascript',
   caption: 'Bringing Facebook to the desktop and mobile web',
   description: (
      'A small JavaScript library that allows you to harness ' +
      'the power of Facebook, bringing the user\'s identity, ' +
      'social graph and distribution power to your site.'
   ),
   link: 'https://developers.facebook.com/docs/reference/javascript/',
   picture: 'http://www.fbrell.com/public/f8.jpg'
  },
  function(response) {
    if (response && response.post_id) {
      alert('Post was published.');
    } else {
      alert('Post was not published.');
    }
  }
);
</script>

通道.html 文件:

<html>
  <head>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
  </head>

  <body>

  </body>

</html>

我想我的主要问题是如何调用此代码以在点击时执行?我会在 fb.ui {} 周围包装一个函数吗?

4

1 回答 1

0

是的,您可以将其包装在一个函数中并调用该函数 onClick。只需在加载时调用 FB.init 部分。以下是我用于发送请求的示例代码。

FB.init({
    appId: '*****************',
    status: true,
    cookie: true,
    xfbml: true
});

function sendRequests() {
    FB.ui({
        method: 'apprequests',
        message: 'Lets play a game.',
    }, function(response) {
    });
}

<a href="#" onclick="sendRequests();return false;">Invite your friends</a>
于 2014-08-12T12:38:01.953 回答