最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Capturing the click event of a Like button - Stack Overflow

programmeradmin0浏览0评论

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/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.

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/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.

Share Improve this question asked Jan 17, 2012 at 2:24 SteveSteve 4,91811 gold badges60 silver badges116 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

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

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.

发布评论

评论列表(0)

  1. 暂无评论