FB评论事件comment.create和comment.remove不起作用



我正在尝试将事件侦听器添加到我的facebook评论中。我尝试了我在Stack Overflow上找到的所有东西,也尝试了FB开发者文档和旧的开发者论坛。评论是正确的,我也可以缓和它们,但事件根本没有被解雇。。。我在一个页面上使用FB评论,带有多个FB:comments 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>

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

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

我认为您应该反转代码片段,因为在尝试初始化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);
        });
};

最新更新