Pankaj Sharma
Pankaj Sharma

Reputation: 679

FB.Event.subscribe code not working

I am using this code to get the response:

<fb:like layout="button_count" notify="true" colorscheme="dark" href="http://www.fbrell.com"></fb:like>
<script>
  // this will fire when any of the like widgets are "liked" by the user
  FB.Event.subscribe('edge.create', function(href, widget) {
    alert('You liked ' + href, widget);
  });
</script>

It's not giving any response. Can anyone tell what wrong am i doing ?

Upvotes: 4

Views: 6482

Answers (3)

Michael L Watson
Michael L Watson

Reputation: 980

Checklist:

(Im assuming you have setup the Javascript SDK)

run the FB linter on your url, errors here may stop it working

http://developers.facebook.com/tools/debug

Include this at the top of the page

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">

If your liking an app, include on your app page

<meta property="fb:app_id" content="Your app id" />

Like button, change href to your link

<fb:like layout="button_count"  show_faces="false" height="21" href="http://facebook.com"></fb:like>

If its an app, FB prefers HTTPS

function init() {
       FB.init({
            appId: APP_ID,
            status: true,
            cookie: true,
            xfbml: true,
            oauth: true
        });

    //uncomment if required 
    //window.fbAsyncInit = function() { 
    FB.Event.subscribe('edge.create',
        function(response) {
            alert('You liked the URL: ' + response);
        }
    );
   //}
}
init();

Upvotes: 0

Rob Church
Rob Church

Reputation: 6943

Assuming that you have the FB Like button appearing, so you must be including the SDK correctly, I was in exactly the same situation - all you're missing is setting the subscribe up in the fbAsyncInit() function.

window.fbAsyncInit = function () {
    FB.Event.subscribe('edge.create', function (targetUrl) {
        _gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
    });
};

If you're doing this you don't need an application ID nor do you you need to use the FBML code, you can use the HTML5 inclusion code like <div class='fb-like' data-href='...

Upvotes: 1

DMCS
DMCS

Reputation: 31870

  1. You're missing the inclusion of the javascript SDK files.
  2. You're missing the FB.init
  3. You're not running your code within the fbAsyncInit() function.

See https://developers.facebook.com/docs/reference/javascript/ for more information.

Upvotes: 1

Related Questions