单击Facebook按钮后不会触发事件? [英] event is not fired after clicking facebook like button?

查看:139
本文介绍了单击Facebook按钮后不会触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让这个事件FB订阅事件被解雇(警告('hi'))但它似乎不起作用:

I am trying to get this event FB Subscribe event fired (alert('hi')) but it does not seem to work though:

<!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: 'myappid', status: true, cookie: true, xfbml: true });
        targetUrl="http://www.goal.com";
        FB.Event.subscribe("xfbml.render", function () {
            console.log('xfbml.render');
            FB.Event.subscribe("edge.create", function (targetUrl) {
                console.log('edge.create');
                alert('hi');    
            });
            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>


推荐答案

我注意到edge.create事件不是'在localhost上触发。上传到Facebook可见的测试服务器后,它运行良好。

I've noticed that edge.create event isn't fired on localhost. After uploading to test server which is visible by Facebook, it worked.

另外,正如Rufinus建议的那样,订阅xfbml.render回调函数之外的那些事件。

Also, As Rufinus suggested, subscribe to those events outside of xfbml.render callback function.

<script>
window.fbAsyncInit = function () {
    FB.init({ appId: 'myappid', status: true, cookie: true, xfbml: true });
    targetUrl="http://www.goal.com";
    FB.Event.subscribe("xfbml.render", function () {
        console.log('xfbml.render');
    });
    FB.Event.subscribe("edge.create", function (targetUrl) {
        console.log('edge.create');
        alert('hi');    
    });
    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>

这篇关于单击Facebook按钮后不会触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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