Capturing the click event of a Like button

吃可爱长大的小学妹 提交于 2019-12-21 18:41:42

问题


I'm trying to subscribe to the Like button click. Here's the code I have:

<body>

<!-- Facebook Like Button with your App ID  -->
<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&appId=349467237487892";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

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

The Like button appears and works ok but I don't get the alert when it's clicked. Does anyone see the problem?

Thanks for your help.


回答1:


You need to attach the event after the JS SDK has had a chance to load. Use fbAsyncInit to do this.

window.fbAsyncInit = function() {

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



回答2:


As far as I can tell from the api you need to subscribe to the edge.create event - example from api documentation.

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

You can read more about FB event subscription here.



来源:https://stackoverflow.com/questions/8888941/capturing-the-click-event-of-a-like-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!