如何仅向具有'点赞'我的网站

How to only display content to visitors that have 'Liked' my website

本文关键字:点赞 我的 网站 何仅      更新时间:2024-01-04

为了解释,我希望只有当访问者"点赞"了我的网站(而不是我的Facebook页面——实际的网站)后,才能点击链接。我一直在尝试使用以下代码:

<script>
  FB.Event.subscribe('edge.create', //FB.event.subscribe attaches a handler to an event and invokes your callback when the event fires.
    function(response) { //This is where the response function is inserted, I used document.write for this example
    document.write(';<a href="#">This is the HTML I want to display</a>');
    }
  );
</script>

但即使在我喜欢我的网站之后,链接也不会显示。我希望我已经尽可能清楚了,任何帮助都将不胜感激。

经过思考和尝试,我成功地使其工作起来:

<!-- FB Javascript SDK -->
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
    appId: 'APP-ID',
    channelUrl : '//WWW.WBESITE.COM/channel.html', // Channel File
    status: true,
    cookie: true,
    xfbml: true,
    oauth: true,
});
// Additional initialization code here
    FB.Event.subscribe('edge.create', function() {
    window.location.href = "http://WEBSITE.COM/";
}); 
};
// Load the SDK Asynchronously
(function(d){
 var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
 if (d.getElementById(id)) {return;}
 js = d.createElement('script'); js.id = id; js.async = true;
 js.src = "//connect.facebook.net/en_US/all.js";
 ref.parentNode.insertBefore(js, ref);
 }(document));
</script>
<!-- More FB Javascript SDK (Like Button) -->
<div id="fb-root"></div>
<script>
(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_US/all.js#xfbml=1&appId=APP-ID";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<script src="//connect.facebook.net/en_US/all.js#xfbml=1">
</script>

FB.Event.subscribe需要放置在window.fbSyncInit函数中。然后我做了这样的设置,即不用在按下"点赞"按钮后显示HTML,只需使用window.location href="http://WEBSITE.COM/";指向包含新链接的页面。document.write函数不起作用,因为它会清除页面上除函数中包含的内容之外的所有HTML。

上面的代码需要放在打开标签之后,然后你可以为"点赞"按钮放置代码:

<div class="fb-like" data-href="http://WEBSITE.COM" data-send="send" data-layout="button_count" data-width="450" data-show-faces="true"></div>

页面上您想要的任何位置!