Search code examples
javascriptjqueryfacebook-like

Calling a function after FB Like button click


I have a facebook application with two like buttons on it. The condition to continue to another page is to like both of them. What i want to do is to call a function after click action on any of these buttons. ( the function could for example reload the page ). Any help would be appreciated.

//edit

so far i've been trying to use jQuery but with no luck

HTML:

<div class="fb-like" data-href="https://www.facebook.com/[some_id]" data-send="false" data-width="450" data-show-faces="false">  
<div class="fb-like" data-href="https://www.facebook.com/[some_id]" data-send="false" data-width="450" data-show-faces="false">  

jQuery:

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

Solution

  • Here is a how you do it:

    FB code of LIKE button

    <div class="social_net_button facebook_button">
          <div id="fb-root"></div>
          <script>(function(d, s, id) {
                  var js, fjs = d.getElementsByTagName(s)[0];
                  if (d.getElementById(id)) {return;}
                  js = d.createElement(s); js.id = id;
                  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
                  fjs.parentNode.insertBefore(js, fjs);
                }(document, 'script', 'facebook-jssdk'));</script>
          <div class="fb-like" data-href=http://www.facebook.com/thebeatles data-send="false" data-layout="button_count" data-width="70" data-show-faces="false" data-action="recommend"></div>
        </div>
    

    And JavaScript code to catch an event

    FB.Event.subscribe('edge.create', function(response) {
     alert('I CLICKED IT');
    });