FB评论事件comment.create和comment.remove不工作 [英] FB comments events comment.create and comment.remove not working

查看:262
本文介绍了FB评论事件comment.create和comment.remove不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Facebook评论中添加事件监听器。我试过可能一切我在这里发现在Stack Overflow,也在FB开发者文档和老开发人员论坛。
注释可以正常工作,我也可以管理他们,但事件不会被解雇...我在一个页面上使用FB评论,与多个fb:评论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-commentshref =myFirstCommentUniqueURLdata-num-posts =2data-width =440notify =truemigrated = >< / fb:comments> 
< fb:comments class =fb-commentshref =mySecondCommentUniqueURLdata-num-posts =2data-width =440notify =truemigrated =1> ; / fb:comment>
< fb:comments class =fb-commentshref =myThirdCommentUniqueURLdata-num-posts =2data-width =440notify =truemigrated =1> ; / fb:comments>
< fb:comments class =fb-commentshref =myFourthCommentUniqueURLdata-num-posts =2data-width =440notify =truemigrated =1> ; / fb:comments>

关于notify =true和migrated =1的提示fb:comments标签参数我在这里找到在堆栈溢出,但他们没有帮助。我也检查了是否没有多个init调用,但它也是整个页面上单独。



所以我不知道,我做错了。 p>

解决方案

我认为你应该反转你的代码片段,因为你正在尝试初始化FB对象后包括所需的JavaScript文件。 / p>

所以代码应该像这样

 < script& 
//包括脚本第一个
(function(d,s,id){
var js,fjs = d.getElementsByTagName(s)[0];
if .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'));


//初始化对象
window.fbAsyncInit = function(){
FB.init({
appId:'{YOURAPPID}',
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);
});

};


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.

解决方案

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

};

这篇关于FB评论事件comment.create和comment.remove不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆