简体   繁体   English

如何检查喜欢的按钮是否已单击

[英]How to check if like button has been clicked

I have a like button on my site, can I check if the user clicked it in the past? 我的网站上有一个喜欢的按钮,我可以检查用户过去是否单击过它吗? I dont want the user will login to facebook through my website, I want to check it without asking for permissions from the user . 我不希望用户通过我的网站登录Facebook,我想检查它而不要求用户许可 Is that possible? 那可能吗? and how can I do it? 我该怎么办?

I think this can be done by saving a cookie when your user clicks like button on your page. 我认为可以通过在用户点击页面上的“喜欢”按钮时保存cookie来实现。 Facebook has some event callbacks of which you can read more here Facebook有一些事件回调,您可以在此处了解更多信息

https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/ https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

The code looks something like this: 代码看起来像这样:

FB.Event.subscribe('edge.create', function(response) {
    if(response) {
        var name = 'UserLikesMyPage'
        var date = new Date();
        date.setTime(date.getTime() + (14 * 24 * 60 * 60 * 1000));
        var expires = '; expires=' + date.toGMTString();
        document.cookie = name + '=true' + expires + '; path=/';
    }
}

Than you can check if this cookie is already set. 比您可以检查此cookie是否已设置。

if (document.cookie.indexOf('UserLikesMyPage') >= 0) {
    alert('I already like your page')
};

And don't forget to remove the cookie if user unlikes your page. 而且,如果用户不喜欢您的页面,请不要忘记删除cookie。 This can be found out by the edge.remove event 这可以通过edge.remove事件找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM