Simple Code Snippet To Get the Number of Your Twitter Followers

Pubblicato il September 4, 2011

Many people often use plug-in or complex piece of code to display the number of their Twitter followers on their websites. Here is a simple code snippet to get your followers number using jQuery and some lines of JavaScript code.

In your page, create a HTML element (div, span, p…) with the ID property set to “followers“:

<div id="followers"></div>

This element will be used to display user’s followers number. Now create a new .js file (twitter.js) with this code:

$(document).ready(function(){
$(function() {
$.ajax({
url: 'http://api.twitter.com/1/users/show.json',
data: { screen_name: 'YOUR SCREEN NAME' },
dataType: 'jsonp',
success: function(data) {
$('#followers').html(data.followers_count.
toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ","));
}
});
});
});

Change ‘YOUR SCREEN NAME’ with your Twitter user name. Than Create a link to jQuery and twitter.js in the <head> tag of the page:

<script type="text/javascript" src="YOUR_PATH/jquery.js" > </script >
<script type="text/javascript" src="YOUR_PATH/twitter.js" > </script >

That’s all! If you know a simpler code please leave a comment.

  • James

    Thanks Antonio, useful and simple.

  • José Padilla

    Or a lil bit smaller(same thing though) http://jsfiddle.net/jpadilla/WmvKY/

  • José Padilla

    What’s the regex for?

    • Antonio Lupetti

      Thousands separator.

  • Ian

    Sweet, thanks! Any API call limits? Last snippet I used for this seemed to count toward calls, sometimes showed a. blank

  • Nicolargo

    The problem with this hack is that every page generate a JSON Query. On my WordPress blog i use the “Subscribers Text Counter” plugin. The counter is generated every hours.

    => http://wordpress.org/extend/plugins/subscribers-text-counter/

  • Hendriono

    Thanks very much Woork Up… Very useful script but simple… I like it..

  • codee47

    Great code, thanks

  • pons

    i think you have to put id=’followers’ and not class=’fallowers’.