How to execute script after a user clicks Like on a Facebook Like button?

痴心易碎 提交于 2019-12-05 17:30:33

You mean you want to execute some Javascript when you like something?

You can use the Facebook Javascript SDK for that, especially the edge.create event.
See

  1. http://developers.facebook.com/docs/reference/javascript/ (for general SDK doc)
  2. http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/ (for event doc)

This should work with the IFrame-version too. If not, you have to switch to the XFBML-Like button. It definetly works with the XFBML version.

Example:

<!-- Load the SDK (or even better, load it asynchronously. See first link above -->
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
// initalize your Facebook App
  FB.init({
    appId  : 'YOUR APP ID',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
  });
// subscribe to the event
FB.Event.subscribe('edge.create', function(response) {
  alert('you liked this');
});
FB.Event.subscribe('edge.remove', function(response) {
  alert('you unliked this');
});
</script>
<!--
    The XFBML Like Button, if the iframe version doesn't work (not 100% sure)
    <fb:like></fb:like>
 -->
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!