User
User

Reputation: 24731

Add javascript function call to Facebook like button

When users click the like button on my site, I want to call a Javascript function.

The source code for the button is:

<div class="fb-like" 
    data-href="http://www.example.com"
    data-layout="button" 
    data-action="like" 
    data-size="large">
</div>

In the browser, that renders to:

<div class="fb-like fb_iframe_widget" data-href="http://www.example.com" data-layout="button" data-action="like" data-size="large" fb-xfbml-state="rendered" fb-iframe-plugin-query="action=like&amp;app_id=&amp;container_width=892&amp;href=http%3A%2F%2Fwww.example.com%2Fm%2F174&amp;layout=button&amp;locale=en_US&amp;sdk=joey&amp;size=large"><span style="vertical-align: bottom; width: 63px; height: 28px;"><iframe name="fb56152a7ff1cc" width="1000px" height="1000px" frameborder="0" allowtransparency="true" allowfullscreen="true" scrolling="no" title="fb:like Facebook Social Plugin" src="https://www.facebook.com/v2.5/plugins/like.php?action=like&amp;app_id=&amp;channel=http%3A%2F%2Fstaticxx.facebook.com%2Fconnect%2Fxd_arbiter%2Fr%2FfTmIQU3LxvB.js%3Fversion%3D42%23cb%3Dfe277476c6182%26domain%3Dlocalhost%26origin%3Dhttp%253A%252F%252Flocalhost%253A8000%252Ff14aab39703836c%26relation%3Dparent.parent&amp;container_width=892&amp;href=http%3A%2F%2Fwww.example.com%2Fm%2F174&amp;layout=button&amp;locale=en_US&amp;sdk=joey&amp;size=large" style="border: none; visibility: visible; width: 63px; height: 28px;" class=""></iframe></span></div>

I've tried:

$('.fb-like').click(function() {
    alert( "Handler for .click() called." );
});

But it doesn't work. I'm wondering if Facebook has some special protection that I don't know about, for editing their buttons?

Upvotes: 1

Views: 1777

Answers (2)

andyrandy
andyrandy

Reputation: 73984

That is not how it works, you need to subscribe to the edge.create event:

FB.Event.subscribe('edge.create', page_like_or_unlike_callback);
FB.Event.subscribe('edge.remove', page_like_or_unlike_callback);

Source: https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

You can put those subscriptions right after FB.init:

window.fbAsyncInit = function() {
    //SDK loaded, initialize it
    FB.init({
        appId      : 'your-app-id',
        xfbml      : true,
        version    : 'v2.8'
    });

    FB.Event.subscribe('edge.create', page_like_or_unlike_callback);
};

More information: http://www.devils-heaven.com/facebook-javascript-sdk-login/

Upvotes: 1

Alexander O&#39;Mara
Alexander O&#39;Mara

Reputation: 60507

There's no special protection, this is just the usual frame behavior and same-origin policy.

If you check the resulting DOM elements, you will see that it creates a iframe, which I have abbreviated to the following:

<iframe ... src="https://www.facebook.com/v2.5/plugins/like.php?..." ...></iframe>

When a user clicks inside that iframe, the event is consumed inside the frame, and your event listener does not fire. Because of same-origin policy, you cannot bind an event inside the frame because it is from a different origin.

You will have to use the JavaScript API Facebook offers to listen to any events it makes available.

Upvotes: 0

Related Questions