我希望我的 FB 应用程序能够自动获取有关用户的信息(如果他们已经登录到 FB)。在 Chrome 中,以下代码完美运行并获取请求的信息。然而,在 Internet Explorer 和 Firefox 中,以下代码仅在我添加 FB.login(); 时才有效。进入window.fbAsyncInit函数。这会导致每次访问页面时都会加载弹出窗口,这是我不想要的。通过使用警报来测试我的代码,我确定 FB.Event.subscribe 函数甚至没有在 IE 和 FF 中被调用,但在 Chrome 中运行良好。为什么这在 IE 和 FF 中表现不同?我怎样才能让它正常工作?
(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));
// Init the SDK upon load
window.fbAsyncInit = function() {
FB.init({
appId : 'ID', // App ID
channelUrl : 'Channel Path', // Path to your Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true,
frictionlessRequests: true
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response){
console.log(response);
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
FB.api('/me', function(me){
if (me.name) {
userID = me.id;
}
});
document.getElementById('auth-loggedout').style.display = 'none';
document.getElementById('auth-loggedin').style.display = 'block';
}else {
// user has not auth'd your app, or is not logged into Facebook
$('#auth-displayname').html('');
document.getElementById('auth-loggedout').style.display = 'block';
document.getElementById('auth-loggedin').style.display = 'none';
}
});
}
最佳答案
只有当您已经登录 Facebook 时,才会调用 auth.statusChange。
我相信您正在 chrome 上进行测试,因此您很可能已登录。但是当您转到 Firefox 或 IE 并且您未登录时 auth.statusChange 永远不会被调用
通过在 chrome 上登录 facebook 进行测试,您会发现它在那里也不起作用。
因此,您必须在页面加载后立即显式调用 FB.getLoginStatus() 来初始化内容,并监听 statusChange 以了解将来的更改:
FB.getLoginStatus(function(response){
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
FB.api('/me', function(me){
if (me.name) {
userID = me.id;
}
})
}
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response){
if (response.authResponse) {
document.getElementById('auth-loggedout').style.display = 'none';
document.getElementById('auth-loggedin').style.display = 'block';
}else {
// user has not auth'd your app, or is not logged into Facebook
$('#auth-displayname').html('');
document.getElementById('auth-loggedout').style.display = 'block';
document.getElementById('auth-loggedin').style.display = 'none';
}
});
关于facebook - FB.Event.subscribe未注册状态:true in Internet Explorer and Firefox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12589228/