问题
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