Web浏览器可以防止Facebook弹出登录窗口
问题描述:
你好,我正在使用这个功能来登录Facebook。现在浏览器禁止弹出窗口打开并请求许可。如何解决它?我可以忽略这个问题,就是浏览器将此视为非用户事件。Web浏览器可以防止Facebook弹出登录窗口
function chk_login()
{
window.fbAsyncInit = function() {
FB.init({appId: my_app_id, status: true, cookie: true, xfbml: true,oauth : true});
FB.getLoginStatus(function(response) {
if (response.authResponse) {
var fb_access_token = response.authResponse.accessToken;
FB.api('/me', function(response) {
console.log(response);
});
}
else
{
}
});
FB.login(function(response) {
if (response.authResponse) {
var fb_access_token = response.authResponse.accessToken;
FB.api('/me', function(response) {
console.log(response);
});
}
},{scope: fb_scope});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
}
<img src="fb_login.jpeg" onclick="chk_login()" alt="Facebook Login" style="cursor:pointer;"/>
答
不要整个包住SDK在你的函数,而不是有你的SDK初始化页面的顶部(在<body>
标签后),并把FB.login()
方法在你的登录功能。类似于:
<body>
<script>
var isLoaded = false;
window.fbAsyncInit = function() {
FB.init({appId: my_app_id, status: true, cookie: true, xfbml: true,oauth : true});
isLoaded = true;
FB.getLoginStatus(function(response) {
if (response.authResponse) {
var fb_access_token = response.authResponse.accessToken;
FB.api('/me', function(response) {
console.log(response);
});
}
});
};
// 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));
function chk_login() {
if(!isLoaded) {
alert("JS-SDK is not yet loaded or something went wrong. Try again.");
return false;
}
FB.login(function(response) {
if (response.authResponse) {
var fb_access_token = response.authResponse.accessToken;
FB.api('/me', function(response) {
console.log(response);
});
}
},{scope: fb_scope});
}
</script>
<img src="fb_login.jpeg" onclick="chk_login()" alt="Facebook Login" style="cursor:pointer;"/>
但是如果我不需要获取用户信息(`/ me`)***每次***页面加载?现在在你编写请求`FB.api('/ me'`在每一页加载/重载的时候都做了,为什么?当用户明确地点击一个按钮“Get User Data”时,我只需要调用这个函数。 – Green 2014-09-12 12:23:06