我正在使用fb登录功能,但是问题就是每当我在页面媒体加载完成之前单击fb登录按钮时,它会阻止fb登录的弹出窗口,但是如果我在第二次传递到fblogin后点击fblogin加载事件它的工作原理
这是我正在使用的功能:
function fb_login() {
var email='';
console.log(loginClassbackQueue);
// console.log('user wants to login with fb');
FB.getLoginStatus(function(response) {
if(response.status!='connected'){
FB.login(function(response) {
// console.log(response);
if (response.authResponse) {
// console.log('user logged in successfully');
// console.log(response);
email = update_f_data_login(response);
$('#fb_login_popup, #popup_overlay').hide();
// loginme(email);
}
else {
loginClassbackQueue = [];
// console.log('user failed to login');
}
// console.log('fb login completed successfully');
}, {scope:"email,user_birthday,user_likes,user_location,friends_likes,publish_actions"}
);
}
else{
// console.log('logged in and connected');
email = update_f_data_login(response);
$('#fb_login_popup, #popup_overlay').hide();
}
});
}
当我在这个网站http://fab.com/上执行相同操作时,打开弹出窗口始终不会阻止弹出窗口。
答案 0 :(得分:55)
您无法从FB.login
的回调中拨打FB.getLoginStatus
。
浏览器倾向于阻止弹出窗口弹出窗口不是由于用户点击操作的直接结果而产生的。
由于FB.getLoginStatus
进行了ajax
通话,并且您对其进行了响应FB.login
,因此此通话打开的弹出窗口已阻止。
问题的解决方案是在页面加载时调用FB.getLoginStatus
并使用fb_login()
方法中的响应。
答案 1 :(得分:40)
只要您确信登录状态已经已在内部加载,就可以从FB.login
的回调中调用FB.getLoginStatus
。为此,请使用以下方法之一:
FB.init({..., status: true, ... })
。FB.getLoginStatus(...)
FB.login(...)
FB.ui(...)
从技术上讲,所有这些选项都使用FB.ui
。异步过程必须完成,这可能需要几秒钟。只要您已经使用上述方法之一与FB进行跨域调用,并且异步进程已完成,获取登录状态将不会进行异步调用,并且弹出窗口不会被阻止。
您还应确保不为第二个参数指定true
,如FB.getLoginStatus(..., true);
中所示。
答案 2 :(得分:12)
确保将状态设置为 true ,这将修复弹出窗口阻止程序问题。
window.fbAsyncInit = function() {
FB.init({
appId : '{your-app-id}',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.5', // use graph api version 2.5
status : true // set this status to true, this will fixed popup blocker issue
});
答案 3 :(得分:0)
我遇到了同样的问题,并且开了3天。我确实偶然发现了上面提到的解决方案,他们在Firefox和Edge工作,但在Chrome中无论我做了什么,我仍然被阻挡在左右中心。另一个问题是我从按钮按下事件调用该功能登录对话框没有阻止但是在登录对话框关闭以进一步操作后它没有得到任何响应,所以我卡住了。所以我的解决方案如下,但您不需要按下登录按钮它将重定向到FB登录页面而没有按钮按下事件,并在返回时继续执行所有其他sdk步骤。只需将其添加到您的代码中,看看它是否有帮助,从那里根据您的需要进行调整
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
document.getElementById('Image2').style.display = "none";
document.getElementById('mail').style.display = "in-line";
testAPI();
} else {
// The person is not logged into your app or we are unable to tell.
window.alert("Faça login no facebook antes de continuar - Obrigado");
window.location.href = 'https://www.facebook.com/dialog/oauth' +
'?client_id=55215442252214521548' +
'&scope=public_profile,email,user_friends' +
'&redirect_uri=' + encodeURIComponent(document.URL);
document.getElementById('Image2').style.visibility = "hidden";
document.getElementById('mail').style.display = "in-line";
}
}
// This function is called when someone finishes with the Login
// Button. See the onlogin handler attached to it in the sample
// code below.
function checkLoginState() {
FB.getLoginStatus(function (response) {
statusChangeCallback(response);
});
}