you can split your code in two files :
Index.php
<button id="fb_login_button" onclick="location.href='facebookLoginPage.php" class="button button-facelogin" > Via Facebook
</button>
facebookLoginPage.php
<script>
FB.login(function(){ /* this is a callback function */ },{scope: 'email,user_birthday,read_stream'});
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXXXXXXXXX', // App ID
channelUrl : '//WEBSITE/index.php', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.authResponseChange', function(response) {
top.location = window.location = "REDIRECTION URL"
if (response.status === 'connected' && $GLOBALS["disconected"]=== "false") {
testAPI();
} else if (response.status === 'not_authorized') {
FB.login();
} else {
FB.login();
}
});
};
(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));
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
}
</script>