Facebook喜欢订阅活动不起作用(Facebook Like Subscribe Event Not Working)

我希望一旦用户点击我们的页面就会触发一个脚本,但我的脚本中没有错误,根本没有结果。 不确定我写错了哪一件事。

<body><div id="fb-root"></div> <script> window.fbAsyncInit = function(){ FB.init({ appId : '213893228799718', status : true, xfbml : true }); FB.Event.subscribe('edge.create', function(response) { alert('You liked the URL: ' + response); }); }; (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_GB/all.js"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); </script> <?php $a = '<iframe src="http://www.facebook.com/plugins/like.php?href='.urlencode('https://www.facebook.com/MyFanPage').'&amp;width&amp;layout=button_count&amp;action=like&amp;show_faces=false&amp;share=false&amp;height=21&amp;appId=213893228799718" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:21px;" allowTransparency="true"></iframe>'; echo $a; ?> </body>

I wish to trigger a script once user click like at our page, but there's no error in my script and no result at all. Not sure which thing I write wrongly.

<body><div id="fb-root"></div> <script> window.fbAsyncInit = function(){ FB.init({ appId : '213893228799718', status : true, xfbml : true }); FB.Event.subscribe('edge.create', function(response) { alert('You liked the URL: ' + response); }); }; (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_GB/all.js"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); </script> <?php $a = '<iframe src="http://www.facebook.com/plugins/like.php?href='.urlencode('https://www.facebook.com/MyFanPage').'&amp;width&amp;layout=button_count&amp;action=like&amp;show_faces=false&amp;share=false&amp;height=21&amp;appId=213893228799718" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:21px;" allowTransparency="true"></iframe>'; echo $a; ?> </body>

最满意答案

如果你仔细阅读文件 ,它说 -

该按钮的XFBML和HTML5版本允许您通过FB.Event.subscribe订阅Facebook SDK for JavaScript中的'edge.create'事件。 只要单击一个按钮,就会触发此JavaScript事件。

你正在使用IFrame版本,这就是它无法正常工作的原因。

此外,它不能与localhost任何版本一起使用,可能是由于某些安全原因。


您可以使用HTML5版本 -

用以下内容替换你的js.src=...行:

js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=213893228799718";

$a = '<iframe sr.... echo $a;

<div class="fb-like" data-href="https://www.facebook.com/MyFanPage" data-layout="button" data-action="like" data-show-faces="true" data-share="true"></div>

If you read the documentation carefully, it says-

The XFBML and HTML5 versions of the button allow you to subscribe to the 'edge.create' event in the Facebook SDK for JavaScript through FB.Event.subscribe. This JavaScript event will fire any time a button is clicked.

And you are using the IFrame version, that's why it isnt working.

Also, it wont work with any version in the localhost, may be due to some security reasons.


You can use the HTML5 version-

Replace your js.src=... line with:

js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=213893228799718";

and

$a = '<iframe sr.... echo $a;

with

<div class="fb-like" data-href="https://www.facebook.com/MyFanPage" data-layout="button" data-action="like" data-show-faces="true" data-share="true"></div>

更多推荐