问题
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.
回答1:
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);
});
};
来源:https://stackoverflow.com/questions/9395789/fb-comments-events-comment-create-and-comment-remove-not-working