javascript - 无法捕捉到用户是否使用“点赞”按钮

标签 javascript html fbconnect

我尝试了很多不同的方法来捕捉一个人是否点击了“喜欢”按钮,但我似乎无法捕捉到点击。

这是 HTM:

<fb:login-button autologoutlink="true"></fb:login-button>
<div id="alertClick" class="fb-like" data-href="https://www.facebook.com/ProbandoRest" data-layout="standard" data-action="like" data-show-faces="true" data-share="false"></div>
<div class="fb-share-button" data-href="https://www.facebook.com/ProbandoRest" data-type="button_count"></div>
<div id="fb-root"></div>
<div
  class="fb-comments"
  data-href="https://www.facebook.com/ProbandoRest"
  data-numposts="5"
  data-colorscheme="light">
</div>

这是 JS:

<script>
(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&appId=699580430058671&version=v2.0";
    fjs.parentNode.insertBefore(js, fjs);
    console.log("comment_callback");
} (document, 'script', 'facebook-jssdk'));

$(document).ready(function () {
    console.log("comment_callback");
    FB.Event.subscribe('edge.create', page_like_or_unlike_callback);
    FB.Event.subscribe('edge.remove', page_like_or_unlike_callback);
    FB.Event.subscribe('auth.authResponseChange', auth_response_change_callback);
    FB.Event.subscribe('auth.statusChange', auth_status_change_callback);
});

$("#auth-logoutlink").click(function () { FB.logout(function () { window.location.reload(); }); });

$("#alertClick").click(function () {
    alert('Aqui se cuenta los shares que se hayan dado por usuario');
    console.log("alert click aqui");
});

FB.api('/me/likes/642246055864172', { limit: 1 }, function (r) {
    if (r.data.length == 1) {
        alert('ya le gustaba al usuario');
    } else {
        alert('Todavia no le gusta al usuario');
    }
});

window.fbAsyncInit = function () {
    FB.init({
        appId: '642246055864172',
        status: false,
        cookie: false,
        xfbml: true
    });

    //Additional
    FB.Event.subscribe('edge.create',
                    function (response) {
                        alert('LIKED: ' + response);
                        console.log("comment_callback");
                    }
             );
};

var comment_callback = function (response) {
    console.log("comment_callback");
    console.log(response);
    alert('Hola regreso el comment');
}

var page_like_or_unlike_callback = function (url, html_element) {
    console.log("page_like_or_unlike_callback");
    console.log(url);
    console.log(html_element);
    alert('Hola regreso el comment');
}
var auth_response_change_callback = function (response) {
    console.log("auth_response_change_callback");
    console.log(response);
}

var auth_status_change_callback = function (response) {
    console.log("auth_status_change_callback: " + response.status);
}

我尝试了 FB Connect 文档中看到的所有方法。以及堆栈溢出的其他一些答案。我使用 C# 在 Visual Studio 中进行编程,我在 FireFox 浏览器中遇到的错误是:

ReferenceError: FB is not defined Info:44
Use of getAttributeNode() is deprecated. Use getAttribute() instead. 8C1Yby4wT_P.js:148
Empty string passed to getElementById().

我不明白为什么当我点击“喜欢”按钮时它没有捕捉到。我发现 fb 未定义是因为在我的应用程序中我仍然没有设置 user_likes 属性,但我不确定。

最佳答案

好的,谢谢我——我把所有东西都放进了 fbAsyncInit 和外面的函数中。这是代码:

window.fbAsyncInit = function () {

    FB.Event.subscribe('edge.create', page_like_or_unlike_callback);

    FB.Event.subscribe('edge.remove', page_like_or_unlike_callback);

    FB.Event.subscribe('auth.authResponseChange', auth_response_change_callback);

    FB.Event.subscribe('auth.statusChange', auth_status_change_callback);

    FB.Event.subscribe('comment.create', comment_callback);

    FB.Event.subscribe('comment.remove', comment_callback);

};

var comment_callback = function (response) {
    console.log("comment_callback");
    console.log(response);
    alert('Hola regreso el comment');
}

var page_like_or_unlike_callback = function (url, html_element) {
    console.log("page_like_or_unlike_callback");
    console.log(url);
    console.log(html_element);
    alert('Hola Like o dejo de likear algo');
}
var auth_response_change_callback = function (response) {
    console.log("auth_response_change_callback");
    console.log(response);
}

var auth_status_change_callback = function (response) {
    console.log("auth_status_change_callback: " + response.status);
}

这解决了我的问题。我把剩下的拿出来只留下函数(d,s,id)。不,我将继续添加我需要在 window.fbAsyncInit 中捕获的内容

关于javascript - 无法捕捉到用户是否使用“点赞”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24373350/

相关文章:

javascript - jquery 在除最后一个变量之外的每个变量之间放置逗号

java - Jsoup:获取文本和网址

ios - FBConnect:如何显示标准 FB 对话框以发布图片和标题?

Facebook 连接错误

javascript - 我正在尝试在 JS 中创建一个函数来搜索 HTML 表中的 <td> 标签,但如果我在其中使用任何 <th></th> 标签,它就不起作用

javascript - 为什么不是 Server.Transfer 为什么只有 Response.Redirect

javascript - 将Apollo-link-error中的Apollo React onError添加到链接const

html - <td> 中的 DIV 向右浮动

javascript - jQuery - onClick 在新页面中打开随机链接

ios - iPhone 中的 fbconnect API 响应