Facebook javascript开火"共享"

Facebook javascript firing on a "share"

本文关键字:quot 共享 javascript 开火 Facebook      更新时间:2023-09-26

我看到了这个链接:

如何检测Facebook分享成功?使用Javascript

但是我怎么实现呢?

首先你需要在你的页面中加载Javascript SDK

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'YOUR_APP_ID', // App ID
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      oauth      : true, // enable OAuth 2.0
      xfbml      : true  // parse XFBML
    });    
  };
  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
</script>

接下来有一个包含FB的函数。打开共享对话框的UI代码。在FB内部。ui函数,您可以看到回调开始的地方function(response) {,其中'response'包含一些细节,帮助您确定用户是否共享消息。

在回调中执行IF语句。如果用户确实发布了消息响应。post_id存在并且包含成功发布消息的id所以我们可以做任何我们想做的,在这个例子中,弹出一个警告说'Post was published'

function share(){
  FB.ui(
    {
      method: 'feed',
      name: 'Facebook Dialogs',
      link: 'http://developers.facebook.com/docs/reference/dialogs/',
      picture: 'http://fbrell.com/f8.jpg',
      caption: 'Reference Documentation',
      description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
      message: 'Facebook Dialogs are easy!'
    },
    function(response) {
      if (response && response.post_id) {
        // THE POST WAS PUBLISHED
        alert('Post was published.');
      } else {
        // THE POST WAS NOT PUBLISHED
        alert('Post was not published.');
      }
    }
  );
}

这里有如何初始化FB Javascript SDK的说明,然后从您的链接使用函数