How to dynamically create Twitter’s tweet button and Facebook’s like button!
I just came across a requirement where I had to create Twitter’s tweet button and Facebook’s like button on the fly.
Doing so is pretty trivial!
Creating the tweet button dynamically
The first thing to do would be to include http://platform.twitter.com/widgets.js.
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
Now consider an example where you have made an ajax request and the response consists of html with links having the class twitter-share-button:
<a href="http://twitter.com/share" class="twitter-share-button">Tweet</a>
Following is how you would go about creating the tweet buttons dynamically:
$.get('ajax/test.html', function(html)
{
var $response = $( html );
// if there is an empty response do nothing
if( $response.length < 1 ) return;
// create the twitter buttons
$response.find('a.twitter-share-button').each(function()
{
var tweet_button = new twttr.TweetButton( $( this ).get( 0 ) );
tweet_button.render();
});
});
Creating the fb like button dynamically
The first thing to do would be including the fb sdk as follows:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function()
{
FB.init({appId: 'your app id', status: true, cookie: true, xfbml: true});
};
(function() {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
Lets again consider the example where you make an ajax request and the response consists of fb like tags, something like following:
<div class="share_button btn_fb"> <fb:like layout="button_count" font="tahoma"></fb:like> </div>
Following is how you would go about creating the fb like button dynamically:
$.get('ajax/test.html', function(html)
{
var $response = $( html );
// if there is an empty response do nothing
if( $response.length < 1 ) return;
// create the facebook buttons
$response.find('.btn_fb').each(function()
{
FB.XFBML.parse( this );
});
});
Although creating these social button is trivial, but I guess the js sdks should have been documented more properly!
-
http://askznatrainer.com/about/twitter-updates-for-2010-11-04 Twitter Updates for 2010-11-04 |
-
http://www.worldspinner.us/twitter World Spinner
-
http://blog.cyberwealthzone.com/?p=7382 Office Quotes | CyberWealthZone Blog
-
http://frankkern.welcometomarketing.com/sponsored-tweets-how-to-get-celebrities-promoting-your-business-opportunity Sponsored Tweets: How To Get Celebrities Promoting Your Business Opportunity! | Welcome To Marketing
-
http://topsy.com/www.ovaistariq.net/447/how-to-dynamically-create-twitters-tweet-button-and-facebooks-like-button/?utm_source=pingback&utm_campaign=L2 Tweets that mention How to dynamically create Twitter’s tweet button and Facebook’s like button! | ovais.tariq — Topsy.com
-
http://brandwaffle.myopenid.com/ Vasken
-
http://neverblog.net/refresh-twitter-and-facebook-social-icons-after-an-ajax-request/ Refresh Twitter and Facebook social icons after an Ajax request « neverblog.net
-
http://www.ovaistariq.net/ Ovais Tariq
-
http://aurimas.eu Anonymous
-
http://www.ovaistariq.net/ Ovais Tariq
-
http://pulse.yahoo.com/_2UWKLFZSQSK6SBSI3EPKCL6FVM William Hawker
-
http://twitter.com/3advance Paul Murphy
-
http://tobias-seckinger.de/2011/06/twitter-tweet-button-nach-ajax-request-nachladen/ Twitter Tweet Button nach Ajax Request nachladen | Tobias Seckinger
-
Kevmc
-
http://www.facebook.com/sahaskatta Sahas Katta
-
Ritch
-
David
-
http://twitter.com/pauldwaite Paul D. Waite
-
Smorto
-
Smorto
-
http://www.comparemyfare.com developer
-
http://www.comparemyfare.com developer
-
http://recovery-review.com decil
-
Digitalpbk
-
http://twitter.com/TomasNeme Tomas Neme
-
http://froddd.co.uk froddd
-
http://www.waterproof-camera.us/ waterproof digital camera
-
Jordan
-
http://ikeif.net keif
-
http://twitter.com/sharmarajukumar raju kumar sharma
-
http://www.smallnotez.com/ Ifat
-
http://twitter.com/ingpazzo Gaetano D’Onghia
-
http://twitter.com/AnthonyVader Anthony Vader
-
http://twitter.com/AnthonyVader Anthony Vader
-
Ezekiel
-
AlphaLemon CMS