FB.Event.subscribe不针对某些事件发射 [英] FB.Event.subscribe not firing for certain events

查看:181
本文介绍了FB.Event.subscribe不针对某些事件发射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在通过以下方式异步创建我的Facebook按钮:

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

这很棒。



具有以下功能:

  window.fbAsyncInit = function(){
FB.init({status :true,cookie:true,xfbml:true});
FB.Event.subscribe(xfbml.render,function(){
console.log('xfbml.render');
FB.Event.subscribe(edge.create ,function(targetUrl){
console.log('edge.create');
});
FB.Event.subscribe(edge.remove,function(targetUrl){
console.log('edge.remove');
});
});
};

到目前为止,当我加载页面时,我在控制台中得到'xfbml.render'。然后我点击像按钮&我没有得到任何东西。



我想要它吐出控制台消息'edge.create'。



<有人有什么想法可能会导致这种情况吗?



我把这个页面放在一个可以公开访问的网站之前(目前在我的开发平台上)它仍然没有工作。

解决方案

为了在您的网页上使用XFBML,您必须向根添加XML命名空间属性你的页面元素。没有这个声明,XFBML标签不会在Internet Explorer中呈现。

 < html xmlns =http://www.w3 .org / 1999 / xhtmlxmlns:fb =https://www.facebook.com/2008/fbml> 

接下来,
您使用标准元素加载SDK并调用(FB.init ))。您还必须在文档中指定一个名为 fb-root 的元素。

 < div id = FB-根 >< / DIV> 

然后确保将您的应用程序ID添加到FB.init()函数中,

  FB.init({
appId:'你的APP ID',
status:true,//检查登录状态
cookie:true,//启用Cookie以允许服务器访问会话
xfbml:true // parse XFBML
});

这是一个完整的工作代码:

 <!doctype html> 
< html lang =enxmlns:fb =https://www.facebook.com/2008/fbml>
< head>
< meta charset =utf-8>
< title> Facebook Like Button< / title>
< / head>
< body>
< div id =fb-root>< / div>
< script>
window.fbAsyncInit = function(){
FB.init({appId:'YOUR_APP_ID',status:true,cookie:true,xfbml:true});
FB.Event.subscribe(edge.create,function(targetUrl){
console.log('edge.create');
});
FB.Event.subscribe(edge.remove,function(targetUrl){
console.log('edge.remove');
});
};
(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);
}());
< / script>
< fb:like>< / fb:like>
< / body>
< / html>


So I'm trying to do some event handling when a user clicks the like button.

My Facebook button is being created asynchronously via:

(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 that works great.

I have the following function running as well:

window.fbAsyncInit = function() {
    FB.init({status: true, cookie: true, xfbml: true});
    FB.Event.subscribe("xfbml.render", function() {
        console.log('xfbml.render');
        FB.Event.subscribe("edge.create", function(targetUrl) {
            console.log('edge.create');
        });
        FB.Event.subscribe("edge.remove", function(targetUrl) {
            console.log('edge.remove');
        });
    });
};

So far, when I load the page, I get 'xfbml.render' in the console. Then I click the like button & I get nothing.

I'd like for it to spit out the console message 'edge.create'.

Does anyone have any idea what could cause this?

I've put this page on a publicly accessible site before (It's currently on my dev rig) & it still didn't work. I can again if requested.

解决方案

In order to use XFBML on your webpage, you must add an XML namespace attribute to the root element of your page. Without this declaration, XFBML tags will not render in Internet Explorer.

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="https://www.facebook.com/2008/fbml">

Next, You load the SDK using the standard element and calling (FB.init()). You must specify a element named fb-root within the document as well.

<div id="fb-root"></div>

and then make sure you add your app id inside FB.init() function,

FB.init({
    appId  : 'YOUR APP ID',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
  });

Here's a full working code:

<!doctype html>
<html lang="en" xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
  <meta charset="utf-8">
  <title>Facebook Like Button</title>
</head>
<body>
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({appId: 'YOUR_APP_ID', status: true, cookie: true,xfbml: true});
    FB.Event.subscribe("edge.create", function(targetUrl) {
      console.log('edge.create');
    });
    FB.Event.subscribe("edge.remove", function(targetUrl) {
      console.log('edge.remove');
    });
  };
  (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);
  }());
</script>
<fb:like></fb:like>
</body>
</html>

这篇关于FB.Event.subscribe不针对某些事件发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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