我正在尝试将事件监听器添加到我的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>
提示有关notify =“true”和migrated =“1”fb:comments标签参数我在Stack Overflow上找到,但是他们没有帮助。我还检查了是否没有多个init调用,但它在整个页面上也是单个。
所以我不知道,我做错了什么。
答案 0 :(得分:3)
我认为你应该反转你的代码片段,因为你在尝试初始化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);
});
};