使用fb.ui创建共享弹出窗口

时间:2013-08-24 22:38:17

标签: javascript facebook api

我无法弄清楚如何成功加载和执行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>

channel.html文件:

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

  <body>

  </body>

</html>

我想我的主要问题是如何调用此代码才能执行点击?我只是在fb.ui {}附近包装一个函数吗?

1 个答案:

答案 0 :(得分: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>