简体   繁体   English

Facebook javascript解雇“分享”

[英]Facebook javascript firing on a “share”

I saw this link here: 我在这里看到这个链接:

How to detect Facebook share success? 如何检测Facebook分享成功? with Javascript 用Javascript

But how do I implement that? 但是我该如何实现呢?

First you need to have the Javascript SDK loaded in your page 首先,您需要在页面中加载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>

Next you have a function that contains the FB.ui code for opening the share dialog. 接下来,您有一个包含用于打开共享对话框的FB.ui代码的函数。 Within the FB.ui function you can see where the callback starts function(response) { , where 'response' contains some details that help you determine if the user did share the message. 在FB.ui函数中,您可以看到回调开始function(response) { ,其中'响应'包含一些可帮助您确定用户是否共享消息的详细信息。

In the callback we do an IF statement. 在回调中,我们做一个IF语句。 If the user did post the message response.post_id exists and contains the id of the successfully posted message so then we can do whatever we want, in this example an alert pops up saying 'Post was published' 如果用户确实发布了消息response.post_id,并且包含成功发布的消息的ID,那么我们可以做任何我们想做的事情,在这个例子中弹出一个警告,说“帖子已发布”

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的说明,然后使用您链接中的函数。

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

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