Facebook喜欢并分享按钮与回调 [英] Facebook like and share button with callback

查看:186
本文介绍了Facebook喜欢并分享按钮与回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,我已经把Facebook和分享按钮和相应的javascript回调函数也存在。



现在的问题是,javascript回调只有在用户喜欢时才会触发,似乎没有触发share按钮被按下。



它像我需要在每次他/她共享链接时授予用户。而喜欢,用户只能喜欢链接一次(不考虑不像/重新样式)。



我知道分享按钮已被弃用。但Facebook现在提供了一个类似按钮的分享按钮。资料来源: https://developers.facebook.com/docs/plugins/like-button/



有没有办法从这个共享按钮触发这个回调方法?



这是我的代码:



我的HTML中BODY标签开头的代码:

 < 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('你喜欢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>

显示Like&分享按钮: -

 < div class =fb-likedata-href =我的网址 data-layout =button_countdata-action =likedata-show-faces =falsedata-share =true>< / div> 


解决方案

您无法使用此按钮获取共享回调。您可以创建一个共享按钮,并在其点击后调用 Feed对话

  FB.ui(
{
method:'feed',
name:'Facebook Dialogs ',
链接:'https://developers.facebook.com/docs/dialogs/',
图片:'http://fbrell.com/f8.jpg',
标题
}
函数(响应){
if(response && response.post_id){
alert('Post was published。');
} else {
alert('Post was not published。');
}
}
);


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>

解决方案

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

这篇关于Facebook喜欢并分享按钮与回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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