About Me

Narendran C R
London, UK
Follow me
Linkedin Facebook Twitter Youtube Blogspot

Saturday, February 19, 2011

Dynamic Facebook Like Button

Recently, some of my friends was asking the simplest way to automatically fetch the url of the current page for Like button. Though this can be done using serverside scripts, the simplest way would be to include it using the javascript. All you need is a javascript file which writes an iframe like button with dynamic url and include that in the html using script tag. Below is the simple example!

Javascript - facebookshare.js
document.write('<iframe scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowtransparency="true" src="' + escape(location.href) + '&layout=button_count&show_faces=true&width=100&action=like&font=segoe+ui&colorscheme=light&height=21"></iframe>')


<title>Facebook Like Button Test</title>
    <script type="text/javascript" src="facebookshare.js"></script>


  1. Long time since I read something technical... Thankfully, I can still understand the above code. Thanks for sharing Naren :)

  2. Is there a code for a dynamic Like button for an app page - so instead of it showing the server app url - it will show the url for the facebook page ie:

  3. Hi Kirk, sorry I don't thing I understand your question correctly. we don't show server app url any where in like button. Are you referring to like box?