簡體   English   中英

用戶在 Facebook 的 Like 按鈕上單擊 Like 后如何執行腳本?

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

我正在嘗試通過 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">

用戶第一次點擊“Like”后,我想在我的 DOM 內容中執行一些 Javascript。 我將如何 go 這樣做?

你的意思是你想在喜歡的時候執行一些 Javascript ?

您可以為此使用 Facebook Javascript SDK ,尤其是edge.create事件。

  1. http://developers.facebook.com/docs/reference/javascript/ (適用於一般 SDK doc)
  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>
 -->

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM