Facebook like and share button with callback

空扰寡人 提交于 2019-11-26 22:37:32

问题


I have a webpage where I have placed a Facebook like & share button and corresponding javascript callback functions are also present.

Now the problem is, the javascript callback fires only when the user 'likes' and doesn't seem to fire when the 'share' button is pressed.

Its like I need to award the user everytime he/she shares the link. Whereas with 'like', a user can only like a link once (not considering unlike/re-like).

I know that the Share button is deprecated. But facebook now provides a share button with a like button. Source : https://developers.facebook.com/docs/plugins/like-button/

Is there a way to fire this callback method from this share button?

Here's my code :

Code at the beginning of the BODY tag in my HTML :-

<div id="fb-root"></div>

<script>
    window.fbAsyncInit = function() {
    FB.init({
        appId: ' My App ID ',
        status: true,
        cookie: true,
        xfbml: true,
        oauth: true
    });
    FB.Event.subscribe('edge.create', function(response) {
        alert('You liked the URL: ' + response);

      });
};
(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>

Code to display the Like & Share Buttons:-

<div class="fb-like" data-href=" My URL " data-layout="button_count" data-action="like" data-show-faces="false" data-share="true"></div>

回答1:


You cannot get the share callback using this like button. You can instead create a share button of yours and invoke the Feed Dialog on its click-

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



回答2:


full code is here

$("#bodyarea").on('click', '.share_fb', function(event) {
    event.preventDefault();
    var that = $(this);
    var post = that.parents('article.post-area');

    $.ajaxSetup({ cache: true });
        $.getScript('//connect.facebook.net/en_US/sdk.js', function(){
        FB.init({
          appId: '822306031169238', //replace with your app ID
          version: 'v2.3'
        });
        FB.ui({
            method: 'share',
            title: 'TTitle Test',
            description: 'Test Description Goes here. Tahir Sada ',
            href: 'https://developers.facebook.com/docs/',
          },
          function(response) {
            if (response && !response.error_code) {
              alert('Posting completed.');
            } else {
              alert('Error while posting.');
            }
        });
  });
});

Change your application id to work

Working Example




回答3:


An alternative way I made here to who don't want to publish an APP just for it is:

   jQuery("#div_share").click(function() {

        var url = "http://www.facebook.com/sharer/sharer.php?u=YOUR_URL&title=YOUR_TITLE";

        var openDialog = function(uri, name, options, closeCallback) {
            var win = window.open(uri, name, options);
            var interval = window.setInterval(function() {
                try {
                    if (win == null || win.closed) {
                        window.clearInterval(interval);
                        closeCallback(win);
                    }
                }
                catch (e) {
                }
            }, 1000);
            return win;
        };

        openDialog(url,'Facebook','width=640,height=580', function(){
            jQuery("#div_share").hide();
            jQuery("#formulario").show();
        });
    });

It will open a javascript dialog to share, and know when the user close it. It's not guaranteed that they really share the content... but.. :)




回答4:


<div class="fb-like" data-href="page_link" data-layout="button_count" data-onshare="page_like_or_unlike_callback" data-onlike="page_like_or_unlike_callback" data-action="like" data-size="large" data-show-faces="true" data-share="false"></div>

Just add data-onlike="function_name" and define the function in your javascript. Your done, your function will be called as soon as the person hits like on your website. For share button it's still in development mode, I'll update you soon on that..



来源:https://stackoverflow.com/questions/23116001/facebook-like-and-share-button-with-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!