解释一下,我希望只有访问者“喜欢”我的网站(不是我的 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.fbAsyncInit 函数中。然后我做了它,而不是让 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>
页面上您想要的任何位置!
关于javascript - 如何只向具有 'Liked' 我的网站的访问者显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11596897/