event is not fired after clicking facebook like button?

走远了吗. 提交于 2019-12-23 02:46:09

问题


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>

回答1:


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

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>


来源:https://stackoverflow.com/questions/8018285/event-is-not-fired-after-clicking-facebook-like-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!