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

发布于 2025-01-08 00:37:36 字数 1669 浏览 0 评论 0原文

我正在尝试将事件侦听器添加到我的 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>

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

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

I am trying adding event listeners into my facebook comments. I tried probably everything I found here on Stack Overflow, also in FB developers docs and old developer forums.
Comments are working correctly, I can also moderate them, but events are not being fired at all... I am using FB comments on one page, with multiple fb:comments FBML tags. Here is my javascript code:

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);
}());

and my 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>

Hints about notify="true" and migrated="1" fb:comments tag parametrs I found here on Stack Overflow, but they did not help. I also checked if there isn't multiple init call, but it is also single on whole page.

So I have no idea, what I am doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄意 2025-01-15 00:37:36

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

};

I think you should reverse your code snippet because you are including the required javascript file after you are trying to initialize the FB Object.

So the code should look like this

<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);
        });

};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文