Facebook的“共享”(javascript)触发 [英] Facebook javascript firing on a "share"

查看:110
本文介绍了Facebook的“共享”(javascript)触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到这个链接:

I saw this link here:

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

但是如何实现?

推荐答案

首先,您需要在您的网页中加载Javascript SDK

First you need to have the Javascript SDK loaded in your page

<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'包含一些细节,可帮助您确定用户是否共享消息。

Next you have a function that contains the FB.ui code for opening the share dialog. 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.

在回调我们做一个IF语句。如果用户没有发布消息response.post_id存在并且包含成功发布的消息的id,那么我们可以做任何我们想要的,在这个例子中,一个警告弹出说发布已发布

In the callback we do an IF statement. 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'

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.');

      }
    }
  );
}

这篇关于Facebook的“共享”(javascript)触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆