Search code examples
facebookfacebook-fqlfacebook-likebox

Facebook Like Box / -Button: React to click


I'm creating a facebook app that should show content only to fans of a certain page. I can easily get the info whether the user is a fan of the page or not using FQL and want to display a Like Box or a Like Button if the user is not already a fan. After the user clicked onto Like, I'd like to display the content that is visible for fans only.

So here's my question: Is it possible to react when the user clicks "Like"? I've tried onclick, but it didn't work.

Here's the code of my Like Box:

<div class="fb-like-box" data-href="https://www.facebook.com/myFanPage" data-colorscheme="light" data-show-faces="false" data-header="false" data-stream="false" data-show-border="false" onclick="alert('TEST');"></div>

And the one for the Like Button:

<div class="fb-like" data-href="https://www.facebook.com/myFanPage" data-layout="standard" data-action="like" data-show-faces="true" data-share="false" onclick="alert('TEST2');"></div>

Thanks very much!


Solution

  • Yes, possible using FB.Event.subscribe() and XFBML

    HTML:

    <fb:like href="{Herf}" width="450"></fb:like>
    

    JS:

    window.fbAsyncInit = function() {
        FB.init({
            appId: '{appID}',
            status: true,
            xfbml: true
        });
    
        // FB event listener 
        FB.Event.subscribe('edge.create', function (url) {
         console.log('You liked the URL: ' + url);
        });
    };
    

    DEMO