The Facebook “Like” button is being added to more and more websites. If you also want to add the “Like” button to your website there are two possible options available. You can either use an iframe directly and generate the code on the Facebook website or use Facebook’s own XFBML extensions to HTML.
Because integration of the iframe solution is really straightforward – you can create your iframe code on the Facebook website – this article will only focus on integrating the XFBML solution which although being a bit more complex has the following advantages:
- allows the user to add a comment as well
- allows you to track the click on the like button
Obtaining A Facebook Application Id
First of all have done this you have to obtain an application id (“app id”) from Facebook just as if you were to create a full Facebook app. Go to this site, enter a name for your “application”, e.g. your website domain name, agree to the terms and click on the “Create” button.
On the following page just enter the URL for your website pointing to your homepage, e.g. “http://www.saschakimmel.com/” in the field “COnnect URL”. Just make sure that the URL contains a slash at the end.
After you’ve clicked on the “Save Changes” button Facebook will show you an application id, your secret key and some more information. For the purpose of this article you only need the application id.
Adding The Core Code With Standard JavaScript
After you have obtained the application id you first of all have to add Facebook’s namespace to your html element on the website you wish to add the “Like” button to:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
After this you have to add this code to the page on your website where you wish to add the “Like” button to. Add this add the very end of your HTML code right before the closing “</body>” tag:
<div id="fb-root"></div> <script type="text/javascript"> <!-- window.fbAsyncInit = function() { FB.init({appId: 'YOUR_FACEBOOK_APP_ID', status: true, cookie: true, xfbml: true}); FB.Event.subscribe('edge.create', function(href, widget) { // Do something, e.g. track the click on the "Like" button here alert('You just liked '+href); }); }; (function() { var e = document.createElement('script'); e.type = 'text/javascript'; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; e.async = true; document.getElementById('fb-root').appendChild(e); }()); //--> </script>
As you can see I have highlighted three parts in red. The first one, “YOUR_FACEBOOK_APP_ID” has to be replaced with the Facebook Application Id you have generated before.
The line beginning with “alert” is where you can add your custom tracking code or whatever action you wish to happen when a user likes your website.
The text “en_US” means “English/US” and defines the locale setting. If you’ve got a German website you would use “de_DE” here etc.
So that’s basically all you have to do to support the button element with standard JavaScript.
Adding The Core Code With Prototype.Js
As you might know I’m using the Prototype.js JavaScript Framework whenever possible so I’ve created a small JavaScript class you can use if you’re using prototype.js on your website.
Just download this file and save it as facebook.js. You can then use this code on your website – just add it to the bottom of your page as well:
<script type="text/javascript" src="facebook.js"></script> <script type="text/javascript"> <!-- Event.observe(window, 'load', function() { new facebookLikeButtonClass('YOUR_FACEBOOK_APP_ID', 'en_US', function(href, widgetObject) { // Do something, e.g. track the click on the "Like" button here alert('You just liked '+href); }); }); //--> </script>
Adding The Button
Now that you’ve setup the core code you can just add the “Like” button wherever you want on your page by adding this code to the page where you want the button to appear:
<fb:like></fb:like>
Now that’s all!
You can set all of the attributes for the “Like” button on the tag as well just like you would set them on the Facebook page for the iframe code.
<fb:like href="URL" layout="standard|button_count" show-faces="true|false" width="450" action="like|recommend" colorscheme="light|dark" font="arial|lucida grande|segoe ui|tahoma|trebuchet ms|verdana"></fb:like>
You can just set the values accordingly. Just make sure you’re really closing the tag with the “</fb:like>”, this separate closing tag must be used and you cannot shorten the tag as you would usually do with the “/>” at the end.
Capturing “Dislikes”
You can’t capture if the user unlikes the site by clicking on the “Like” button again. The Facebook JavaScript API just doesn’t provide any event for this.