我对 javascript 很陌生,真的不明白该怎么做,Facebook 登录在任何浏览器中仍然被弹出窗口拦截器阻止
我将此代码放入文件 fbapp1.php
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'myappid', // App ID
channelUrl : '//example.com/fbapp1.php', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// this shouldn't be called directly, but instead should be initiated with a user click event
FB.login(function(response) {
if (response.authResponse) {
// document.write ('Access Token: ' + response.authResponse.accessToken);
window.location = 'https://example.com/fbapp2.php?access_token=' + response.authResponse.accessToken;
} else {
alert ('User cancelled login or did not fully authorize.');
}
});
};
// 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>
为了打开这个文件,我在另一个页面中使用简单的 html href 代码
<a href="https://example.com/fbapp1.php" target="_blank">"Pulsa Fortune App"</a>
但是 Facebook 登录弹出窗口总是被浏览器阻止。上面的代码已经告诉了这个事情,但我不知道它的意思是什么?
this shouldn't be called directly, but instead should be initiated with a user click event
请告诉我我的代码应该如何?
最佳答案
不要在异步回调函数中调用FB.login
- fbAsyncInit
是异步的。您必须在用户交互时调用它,否则浏览器将有充分的理由阻止它。
示例:
document.getElementById('loginBtn').addEventListener('click', function() {
//do the login
FB.login(function(response) {
if (response.authResponse) {
//user just authorized your app
document.getElementById('loginBtn').style.display = 'none';
getUserData();
}
}, {scope: 'email,public_profile', return_scopes: true});
}, false);
来源:http://www.devils-heaven.com/facebook-javascript-sdk-login/
关于javascript - Facebook 登录弹出窗口被浏览器阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48426924/