Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook like and share button with callback

Tags:

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> 
like image 745
Krish Avatar asked Apr 16 '14 17:04

Krish


2 Answers

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.');   } } ); 
like image 166
Sahil Mittal Avatar answered Oct 06 '22 23:10

Sahil Mittal


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

like image 39
Muhammad Tahir Avatar answered Oct 07 '22 00:10

Muhammad Tahir