The following code should be placed directly after the opening <body> tag on your web page.
Here is a breakdown of how the following code blocks function:
» FB.init: this function initializes the Facebook API (events, etc.)
» FB.Event.subscribe: these 2 functions register the events which trigger the login and logout functions of Facebook API and will run when either a login or logout from Facebook has been triggered.
» FB.getLoginStatus: this function gets the initial login status when the page is loaded and sets the customized layout depending on whether the user is logged into Facebook already.
» login/logout: these functions are triggered by the above functions as noted in the code below. I have utilized them for proper code reusability.
Note: do not forget to replace "YOUR_APP_ID" with your own Facebook app id.<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '147777435313',
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.login', function(response) {
//if user is logged in
login();
});
FB.Event.subscribe('auth.logout', function(response) {
//if user is logged out
logout();
});
FB.getLoginStatus(function(response) {
if (response.session) {
//if user is logged in
login();
} else {
//if user is logged out
logout();
}
});
};
//if user is logged in - do this
function login() {
FB.api('/me', function(response) {
document.getElementById('fb-info-block').innerHTML =
"Login successful! Welcome, " + response.name + ".<br /><br />" +
"<fb:like href = 'www.whitbreaddesign.com' show_faces = 'false' width = '100' action = 'like' colorscheme = 'light'></fb:like>";
});
}
//if user is logged out - do this
function logout() {
document.getElementById('fb-info-block').innerHTML =
"Please log in using Facebook to see the welcome message!";
}
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
Comments