Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capture click of Facebook 'Like' button with jQuery

I am looking on the Facebook Developers Documentation to locate some sample code that would allow me to capture a click event on the "Like" button. It states:

If you are using the XFBML version of the button, you can subscribe to the 'edge.create' event through FB.Event.subscribe.

I am not using XFBML however, this is my code:

<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);

              FB.Event.subscribe('edge.create',
                     function(response) {
                          alert('You liked the URL: ' + response);
                     }
              );

            }(document, 'script', 'facebook-jssdk'));</script>
      <div class="fb-like" data-href="http://www.facebook.com/pages/Ayrshire-Minis/160330240663397" data-send="false" data-layout="button_count" data-width="70" data-show-faces="false" data-action="recommend"></div>
    </div>

Can a click of the 'Like' button be captured by jQuery?

like image 987
crmpicco Avatar asked Aug 01 '12 11:08

crmpicco


1 Answers

You could try this.

http://jsfiddle.net/qkyAe/

Docs: http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

FB.Event.subscribe('edge.create', function(response) {
    console.log('clicked');
});​

I think this one only counts if user clicks AND it increaes the like count (logged in users only) Since I don't have an FB-Account I couldn't try it. Please tell me, if it works :)

Update:

The FB object comes from the script you're already embedding.

like image 117
Robin Drexler Avatar answered Sep 19 '22 11:09

Robin Drexler