如何在用户点击Facebook赞按钮后执行脚本?

Pus*_*dra 4 jquery facebook facebook-social-plugins

我试图通过iFrame使用Facebook Like按钮,如下所示:

<iframe id="test" src="http://www.facebook.com/plugins/like.php? 
       href=http://yoururl.com/&amp;
       layout=button_count&amp;
       show_faces=false&amp;
       width=50&amp;
       action=like&amp;
       colorscheme=light&amp;
       height=21" 
       scrolling="no" frameborder="0" 
       style="border:none; overflow:hidden; width:50px;  
              height:21px;" 
       allowTransparency="true">
Run Code Online (Sandbox Code Playgroud)

用户第一次点击"赞"后,我想在我的DOM内容中执行一些Javascript.我该怎么做呢?

Sim*_*ker 5

你的意思是你想在你喜欢的时候执行一些Javascript吗?

您可以使用Facebook Javascript SDK,尤其是edge.create事件.
看到

  1. http://developers.facebook.com/docs/reference/javascript/(适用于一般SDK文档)
  2. http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/(适用于活动文档)

这也适用于IFrame版本.如果没有,您必须切换到XFBML-Like按钮.它绝对适用于XFBML版本.

例:

<!-- 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>
 -->
Run Code Online (Sandbox Code Playgroud)

  • 对于它的价值,它应该在你的`body`的*bottom*之后,在`<fb:like>/<iframe>`之后.我不记得让它与iframe一起使用,但我可能错了.XFBML绝对有效. (2认同)