我创建了一个主要用 jQuery 构建的 Facebook 应用程序。我现在需要检查用户是否喜欢特定页面。但是,这需要添加到 jQuery click() 函数中(因此,如果用户单击该按钮,它将给出一个对话框,表明他们在不喜欢该页面的情况下无法继续)并有效地停止脚本运行。
我的整个应用程序都使用 jQuery,它不是不同的页面,所以我确实需要 jQuery 和 Facebook JSDK 之间的交互,但我不确定集成将如何工作。有人有有用的资源供我查看吗?或者也许是一个脚本:)
最佳答案
使用这个:
$(document).ready(function() {
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelURL : '//www.YOUR_DOMAIN.com/channel', // Channel File URL
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.authResponse) {
// logged in and connected user, someone you know
var user_id = response.authResponse.userID;
var page_id = "YOUR_PAGE_ID";
var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
var the_query = FB.Data.query(fql_query);
the_query.wait(function(rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
//user likes the page
//do your stuff here
} else {
// user doesn't like our page yet
}
});
} else {
// no user session available, someone not known, not logged into fb
}
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
});
并在您的 html/jsp/php 中添加以下行:
<div id="fb-root"></div>
channel 文件不是必需的,但建议使用。查看 Facebook 文档以获取更多信息。
关于javascript - Facebook Javascript-SDK 检查用户是否喜欢某个页面的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8052364/