I have the following JavaScript code
<div class="fb-like-box" data-href="snip" data-colorscheme="light"
data-show-faces="false" data-header="false" data-stream="false"
data-show-border="true"></div>
I am trying to detect when the like button is clicked. The above code renders an iframe so my jquery event handler of
$('.fb-like-box > button').on('click', function() { console.log('Clicked'); });
Never fires. I tried wrapping this div and other content in a wrapper div and target that in the $() call but it only fires when the other content is clicked, not the area that is rendered by the Facebook SDK.
I have the following JavaScript code
<div class="fb-like-box" data-href="snip" data-colorscheme="light"
data-show-faces="false" data-header="false" data-stream="false"
data-show-border="true"></div>
I am trying to detect when the like button is clicked. The above code renders an iframe so my jquery event handler of
$('.fb-like-box > button').on('click', function() { console.log('Clicked'); });
Never fires. I tried wrapping this div and other content in a wrapper div and target that in the $() call but it only fires when the other content is clicked, not the area that is rendered by the Facebook SDK.
Share Improve this question asked Apr 7, 2015 at 16:44 andrewbandrewb 3,0958 gold badges58 silver badges101 bronze badges 4- You can't. Why would you want to do that anyway? – WizKid Commented Apr 7, 2015 at 16:48
- Because I need to record this action and give the user a 'point' as a reward. Points can used to redeem products/prizes on the site. – andrewb Commented Apr 7, 2015 at 16:49
- That is not allowed by Facebook Platform Policy – WizKid Commented Apr 7, 2015 at 16:51
- You can periodically call the Graph API in order to get the count of likes. However you cannot track whether user clicked on the button or not. But you can search something like "clickjacking" if your REALLY want to do it (it is not allowed and you can be banned, btw). Also you can put an invisible element under the cursor and move it (element) using JS. – arm.localhost Commented Apr 7, 2015 at 19:20
3 Answers
Reset to default 5https://developers.facebook./docs/reference/javascript/FB.Event.subscribe/v2.3
You just need to subscribe to the edge.create
event for likes. See docs for example code.
But as mented, you are not allowed to reward users for liking. Read the platform policy for more information: https://developers.facebook./policy/
Update: https://developers.facebook./blog/post/2017/11/07/changes-developer-offerings/
edge.create and edge.remove JS SDK Events: These Events will no longer be accessible. As an alternative, you can use our Webhooks and be notified every time someone likes one of your Facebook Pages.
This code works for me:
<script>
var liked_page = function() {
alert("liked!");
}
FB.Event.subscribe('edge.create', liked_page);
</script>
I have placed it at the end of the page. Furthermore I believe you should also place
<div id="fb-root"></div>
At the beginning of the page, right after the <body> tag.
edge.create and edge.remove JS SDK Events: These Events will no longer be accessible. As an alternative, you can use our Webhooks and be notified every time someone likes one of your Facebook Pages.
https://developers.facebook./blog/post/2017/11/07/changes-developer-offerings/