我的网站上有这个代码。当我的页面加载时显示facebook登录对话框,而不是当用户点击锚标记时。
<script>
// Additional JS functions here
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxx', // App ID
channelUrl : '//192.168.1.127/test/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional init code here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected
} else if (response.status === 'not_authorized') {
// not_authorized
login();
} else {
// not_logged_in
login();
}
});
};
function login() {
FB.login(function(response) {
if (response.authResponse) {
testAPI() ;
} else {
// cancelled
}
});
}
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
}
// 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>
我希望每当用户点击此内容时都会触发FB.getLoginStatus
事件:
<a href = "#" onClick = "FB.getLoginStatus()">Login</a>
但是现在它正在装载而没有我做任何事情。
建议?
答案 0 :(得分:8)
好的,这里有一个简单的解决方案 - 在另一个函数中包含对FB.getLoginStatus
的调用,并让你的按钮调用 函数。
function doLogin(){
FB.getLoginStatus(function(response) {
...
};
}
然后,您可以将按钮上的onClick
更改为doLogin()
。
Bassicaly,这里发生的事情是,一旦加载了Facebook SDK,它就会调用包含window.fbAsyncInit
代码的FB.getLoginStatus
函数,因此只要SDK准备就绪,就会登录代码正在执行。