当javascript函数完成时,我似乎对正确的顺序有一些问题。我有一个页面可以检查用户是否登录到 facebook。如果有人登录,它将注销该人,并重定向到包含“赞”按钮的不同页面。但是,当我在没有重定向的情况下运行网页时,用户会正确注销。如果重定向到位,则不再强制注销。
你可以在这个网站上测试代码:http: //quiet-depths-9481.herokuapp.com
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"> <html> <head> <script src="src/jquery.js"></script> </head> <body style="background-color:#ff6600"> <div id="fb-root"></div> <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script> <script type="text/javascript"> var loggedout = false; window.fbAsyncInit = function() { FB.init({ appId : 'xxxxxxxxxxxxx', // App ID channelUrl : '', // Channel File status : true, // check login status cookie : true, // enable cookies to allow the server to access the session xfbml : true // parse XFBML }); fbApiInit = true; }; (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/nl_NL/all.js#xfbml=1&appId=511896318825154"; ref.parentNode.insertBefore(js, ref); }(document));
function fbEnsureInit(callback)
{
if(!window.fbApiInit) {
setTimeout(function() {fbEnsureInit(callback);}, 50);
} else {
if(callback) {
callback();
}
}
}
function fbEnsureLogout(callback)
{
if(!window.loggedout) {
setTimeout(function() {fbEnsureLogout(callback);}, 50);
} else {
if(callback) {
callback();
}
}
}
fbEnsureInit(function()
{
console.log("this will be run once FB is initialized");
checkLogout();
});
fbEnsureLogout(function()
{
console.log("this will run if logout is ensured");
window.location = "http://quiet-depths-9481.herokuapp.com/like.php"
document.write('<div class="fb-like" data-href="https://www.facebook.com/accentjobs" data-width="450" data-show-faces="true" data-stream="true" data-header="true"></div>');
});
function checkLogout()
{
console.log('checkLogout');
FB.getLoginStatus(function(response)
{
if (response.status === 'connected')
{
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
console.log('logged in & authenticated');
console.log('trying to logout now.');
FB.logout(function(response)
{
console.log('LOGGED OUT!');
loggedout = true;
});
}
else if (response.status === 'not_authorized')
{
console.log('logged in but no authentication');
console.log('trying to logout now.');
FB.logout(function(response)
{
console.log('LOGGED OUT!');
loggedout = true;
});
}
else
{
console.log('Not logged in to facebook!');
loggedout = true;
}
});
}
</script>