skip to main | skip to sidebar

Combining Your Trending Stories with Twitter Trackbacks

This is a simple jQuery tutorial to display Twitter trending stories of your website and their Twitter trackbacks by combining 2 of my jQuery plugins - Popular on Twitter Widget & Twitter Trackbacks Widget.

Both plugins are based on Topsy awesome API which can return the most popular stories of your website on Twitter within the desired period of time. Popularity is calculated by the influence of people talking about those stories. Topsy also keeps a complete history of Twitter trackbacks of your stories which are simply the tweets that mention it.


1- Add A Placeholder Div
Create a HTML div tag with ID "popular".
<div id="popular">loading..</div>
Also add CSS code from demo page which is a combination of the 2 widgets CSS..

2- Include Javascript files of jQuery, Popular on Twitter Widget and Twitter Trackbacks Widget
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>

3- Call Popular-on-Twitter Widget when document ready
   // call popular on Twitter widget on document ready
      site: ''
      ,show_n: 0
      ,window: 'a'
      ,n: 5
      ,show_tweet: 0
      ,tweeter_text: '<\a href="#" class="get-trackbacks" title="Expand/Collapse tweets">+<\/a> by _d_'
      ,loaded: popularLoaded

Called the popular-on-Twitter widget on placeholder div with few widget options:

  • Set "site" to my blog domain.
  • Set "window" to "a" to get popular links in all time - you can use "m" for last month, "w" for last week or "d" for last day.
  • Set "show_n" to 0 to display links as fixed list without any transitions.
  • Set "n" to 5 to limit number of links to 5.
  • Added a + link button with class "get-trackbacks" inside the "tweeter_text" option. To get Twitter trackbacks when user clicks it.
  • Set "loaded" to function pointer "popularLoaded" that will be called when popular links are loaded.

4- Finally, Few jQuery Magic
function popularLoaded(){
   var div = $('#popular');
   // for each link li
      var li = $(this);
      // get url of the link
      var url = $('.pot-url a',li).attr('href');
      // bind click handler to the + link
         var a = $(this);
         // trackbacks div
         var tb_div= $('div.trackbacks',li);
         // trackbacks div is there ?
         if(tb_div.size()==0) {
            // create trackbacks div for the first call
            tb_div= $('<\div class="trackbacks"><\/div>').appendTo(li);
            // call trackbacks widget
            // change + link to -
            // div was created
            if( tb_div.css('display')=='none') {
               // show if hidden
               // change + link to -
            }else {
               // hide if shown
               // change - link to +
         // return false to disable default behavior of link click
         return false;

This function will be called when popular links are loaded and appended to placeholder as unsorted list items..

  • Traversing the unsorted list items.
  • Get link's URL inside each list item.
  • Define a click event handler for the + button that was just added in previous call to Popular-on-Twitter widget.
  • Find trackbacks div inside current list item -identified by its class "trackbacks".
  • If not found -jQuery object size is zero- then will append trackbacks div to current list item. Then call Twitter trackbacks widget with few options:
    • Set "url" to current link url.
    • set "n" to 5 to limit number of trackbacks.
    • set "show_n" to 0 to display trackbacks as a fixed list.
  • Else, div was found -trackbacks widget was called before. Then fade it in if it wasn't shown and vice versa.
  • Note that the + button text is set to "-" when trackbacks is shown and reset to "+" otherwise.

Each widget comes with many other options that were not used here in this simple demo. For more info refer to:
Popular on Twitter > options
Twitter Trackbacks > options



  1. web designer // November 30, 2010 at 11:09:00 PM GMT+11  

    wonderful.. I have been looking for this. Now it will be interesting to use j Query.Thanks for sharing the information.

  2. BLOUZO LTD Web Design // January 28, 2011 at 9:46:00 AM GMT+11  

    Neat use of jQuery & Twitter feeds. Always interesting to see where various web designers take jQuery to in their experiments!

  3. Zeth // January 31, 2011 at 10:51:00 PM GMT+11  

    Aahhh. Javascript keeps surprising me. I'm a PHP-man myself and I keep thinking if I should have gone the other way? What do you reckon? PHP vs Javascript, 1on1 - who'd win?

    Good Blog by the way. You're a good writer!

Post a Comment

Thank you for taking the time to comment..
* If you have a tech issue with one of my plugins, you may email me on mike[at]
More Tech Tips! | Technology tips on web development


Mike MoreWeb developer, jQuery plugin author, social media fan and Technology Blogger.
My favorite topics are: jQuery , Javascript , ASP.Net , Twitter , Google..
<connect with="me"> </connect>

Subscribe by email

Enter your email address:

or via RSS