2012-07-22 72 views
2

爲了解釋,我想要一個鏈接,只有當訪問者點擊'喜歡'我的網站(不是我的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> 

但即使在我喜歡我的網站後,鏈接也不會顯示。我希望我已經儘可能清楚,任何幫助將不勝感激。

+1

問題是什麼? – 2012-07-22 00:35:27

+0

我喜歡頁面後不顯示。 – Payne 2012-07-22 00:39:18

+0

您是否檢查過回調是否實際被調用? – jeff 2012-07-22 00:43:20

回答

0

我設法得到它後,一些思想工作和玩耍:

<!-- 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函數中。然後我做了這樣的工作,以免在按下'Like'按鈕後出現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> 

任何地方,你想在頁面上!

相關問題