3 javascript facebook facebook-javascript-sdk
我在我的网站上有这个代码.当我的页面加载时显示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>
Run Code Online (Sandbox Code Playgroud)
我希望FB.getLoginStatus每当用户点击此事件时都会触发该事件:
<a href = "#" onClick = "FB.getLoginStatus()">Login</a>
Run Code Online (Sandbox Code Playgroud)
但是现在它正在装载而没有我做任何事情.
建议?
好的,这里有一个简单的解决方案 - 将调用包装FB.getLoginStatus在另一个函数中,让你的按钮调用该函数.
function doLogin(){
FB.getLoginStatus(function(response) {
...
};
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以onClick将按钮更改为doLogin().
Bassicaly,这里发生的事情是,一旦加载了Facebook SDK,它就会调用window.fbAsyncInit包含你FB.getLoginStatus代码的函数,所以一旦SDK准备就绪,登录代码就会被执行.
| 归档时间: |
|
| 查看次数: |
8573 次 |
| 最近记录: |