【问题标题】:FB comments events comment.create and comment.remove not workingFB评论事件comment.create和comment.remove不起作用
【发布时间】:2012-03-12 20:08:52
【问题描述】:

我正在尝试将事件侦听器添加到我的 facebook cmets 中。我可能尝试了我在 Stack Overflow、FB 开发者文档和旧开发者论坛中找到的所有内容。 评论工作正常,我也可以调节它们,但根本没有触发事件......我在一页上使用 FB cmets,带有多个 fb:cmets FBML 标签。这是我的javascript代码:

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

    FB.Event.subscribe('comment.create',
        function (response) {
            console.log('create', response);
        });
    FB.Event.subscribe('comment.remove',
        function (response) {
            console.log('remove', response);
        });

};
(function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
}());

还有我的 HTML:

<fb:comments class="fb-comments" href="myFirstCommentUniqueURL" data-num-posts="2" data-width="440"  notify="true"  migrated="1"></fb:comments>
<fb:comments class="fb-comments" href="mySecondCommentUniqueURL" data-num-posts="2" data-width="440"  notify="true"  migrated="1"></fb:comments>
<fb:comments class="fb-comments" href="myThirdCommentUniqueURL" data-num-posts="2" data-width="440"  notify="true"  migrated="1"></fb:comments>
<fb:comments class="fb-comments" href="myFourthCommentUniqueURL" data-num-posts="2" data-width="440"  notify="true"  migrated="1"></fb:comments>

关于 notify="true" 和 migrated="1" fb:cmets 标签参数的提示是我在 Stack Overflow 上找到的,但它们没有帮助。我还检查了是否没有多个 init 调用,但它在整个页面上也是一个。

所以我不知道,我做错了什么。

【问题讨论】:

  • 如果您解决了问题,请自行回答并标记为正确。另外我建议Shift+F5(刷新+绕过缓存)

标签: facebook events comments


【解决方案1】:

我认为你应该反转你的代码 sn-p 因为你在尝试初始化 FB 对象后包含了所需的 javascript 文件。

所以代码应该是这样的

<script>
//INCLUDE THE SCRIPT FIRST
(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/all.js#xfbml=1&appId={YOURAPPID}";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));


//INITIALIZE THE OBJECTS
window.fbAsyncInit = function() {
    FB.init({
        appId:  '{YOURAPPID}',
        status: true,
        cookie: true,
        xfbml:  true,
        oauth: true
    });

    //AND THOSE WILL FIRE THE EVENTS :)

    FB.Event.subscribe('comment.create',
        function (response) {
            console.log('create', response);
        });
    FB.Event.subscribe('comment.remove',
        function (response) {
           console.log('remove', response);
        });

};

【讨论】:

  • 是的,你是对的,它应该在代码中更高,但它是双向的。无论如何,我将它移到了 init 函数之上。谢谢指出。
猜你喜欢
  • 2019-07-01
  • 2013-07-10
  • 2013-02-04
  • 1970-01-01
  • 2012-06-19
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多