如何解决用户在Facebook“赞”按钮上单击“赞”后如何执行脚本?
| 我试图通过iFrame使用Facebook Like按钮,如下所示:<iframe id=\"test\" src=\"http://www.facebook.com/plugins/like.php?
href=http://yoururl.com/&
layout=button_count&
show_faces=false&
width=50&
action=like&
colorscheme=light&
height=21\"
scrolling=\"no\" frameborder=\"0\"
style=\"border:none; overflow:hidden; width:50px;
height:21px;\"
allowTransparency=\"true\">
用户首次单击“赞”后,我想在DOM内容中执行一些Javascript。我将如何去做呢?
解决方法
您的意思是您想在喜欢时执行一些Javascript吗?
您可以为此使用Facebook Javascript SDK,尤其是
edge.create
事件。
看到
http://developers.facebook.com/docs/reference/javascript/(适用于常规SDK文档)
http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/(针对事件doc)
这也应该适用于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>
-->