GissiSim.com

Gissur Simonarson Online

Live jQuery Twitter feed

As many of you that read my blog already know, I am in love with twitter. It has opened up a world for me that I just didn’t know existed, really given me the love I once had for the internet back. Since I’ve gotten so much from it, I’ve decided to give something back to the community. Since not everyone is a big coder, I want to give those of you that want a live twitter feed on your page the code to accomplish that.

Please feel free to use this code for your own personal use. However if you want to sell this or use it on a corporate site, please talk to me for permission. Also if you guys want to give me some expert tips on how to make things better in the code, please comment on what can be changed or added! Thank you.

Link straight to example.

The Code

<script type="text/javascript">
      $(
          function()
          {
              setInterval("getSearch()",10000);
          getSearch();

          }
      );

    TwitterCache = {};

  function getSearch()
  {

            var url = "http://search.twitter.com/search.json?q=%22in+love%22&callback=?";
            // Change %22in+love%22 to your search. %22 is a qoute
            $.getJSON
            (
                url,
            function(data)
            {
                if( data.results ) // Checks to see if you have any new tweets
                {
                    var i = -1, result, HTML='', HTML2='';
                    while( (result = data.results[++i]) && !TwitterCache[result.id] )
                    {
                        TwitterCache[result.id] = result.from_user;
                        HTML += '<li id="TwitterId'+ result.id +'"><img src="' +
                        result['profile_image_url']+'" alt="Profile Image" />
                        <strong class="User">'+ result.from_user + '</strong>' +
                        FormatIt(result.text) + ' <span class="created_at">' +
                        relative_time(result.created_at) + '</span> via ' +
                        linkup(result.source) + '</li>';

                    }
                    if( HTML !== '' )
                        $("#tweets ul").prepend( HTML ).find('li:hidden').slideToggle('slow');

                    $("li:gt(14)").remove(); // Removes everything past the 15th tweet

                    $("li:even").css  // Simple Styling of the boxes START
                        ({
                            "background": "#DFDFDF",
                            "border-color": "#DFDFDF",
                            "border-style": "solid",
                            "border-width": "2px"
                        });
                    $("li:odd").css
                        ({
                            "background": "grey",
                            "border-color": "grey",
                            "border-style": "solid",
                            "border-width": "2px"
                        });
                    $("li").hover(
                      function () {
                        $(this).addClass("hover");
                        $(this).animate({
                            opacity: 1.0
                            }, 10);

                      },
                      function () {
                        $(this).removeClass("hover");
                        $(this).animate({
                            opacity: 0.2
                            }, 10);
                      }
                    ); // STYLING ENDS

                }
            }
        )

  }

    function FormatIt(text_results)  // Formats the text. Makes In Love become bold
    {
        text_results = text_results.replace(/(in love)/ig,
        '<strong class="inlove">$1</strong>');
        return text_results.linkify();
    }

    function linkup(link) // Makes the links linkable
    {
        link = link.replace('&lt;a href=&quot;','<a href="');
        link = link.replace('&quot;&gt;','">');
        link = link.replace('&lt;/a&gt;','</a>');
        return link;
    }

      String.prototype.linkify = function() {  //Also makes links linkable
            return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/~.=]+/,
            function(m) {
        return m.link(m);
      });
    };

  function relative_time(time_value) { //Makes the time thing happen
        var values = time_value.split(" ");
        time_value = values[2] + " " + values[1] + ", " + values[3] + " " + values[4];
        var parsed_date = Date.parse(time_value);
        var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
        var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
        delta = delta + (relative_to.getTimezoneOffset() * 60);

        var r = '';
        if (delta < 60) {
          r = 'a minute ago';
        } else if(delta < 120) {
          r = 'couple of minutes ago';
        } else if(delta < (45*60)) {
          r = (parseInt(delta / 60)).toString() + ' minutes ago';
        } else if(delta < (90*60)) {
          r = 'an hour ago';
        } else if(delta < (24*60*60)) {
          r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
        } else if(delta < (48*60*60)) {
          r = '1 day ago';
        } else {
          r = (parseInt(delta / 86400)).toString() + ' days ago';
        }

        return r;
}
function twitter_callback ()
{
      return true;
}

</script>

The Example

This is probably the best way to see how you should make this work. Just look at the code and you should get a fairly good idea what I’m doing. I’ve put comments next to the things that can be changed in the jQuery code. It’s all in one HTML file to make it easier to see all at once.

jquerytwitter

Link to exmaple.

Bookmark and Share

No related posts.



Tagged as , , , , , , + Categorized as Labs, Twitter
  • This is just what I was looking for, thanks.!
    just started to work on :
    < a href="#" onclick="getSearch();">search< /a >
    with additional text area. I'll get back with it hopefully. Thanks again.
  • Ammy
    Hi, I downloaded and used coda’s jquery twitter plugin. It is easy to style using CSS and I like it. But then I ended up using this jquery twitter API plugin to create my own and customized twitter badge.
  • Gilad
    Question: what should be changed in the code above if I want the link created by linkify or limkup to have target="_blnak" attribute?

    Tx,
    Gilad
  • I'd like to discuss using for corporate use.

    Cheers
  • Thanks so much for this! I've been waiting for this for a long time.

    I have a working example of this on my blog that displays anyone talking about me on twitter, whether it be responses, retweets, or even my own tweets. you can see the example here:

    http://infidelsarecool.com/twitter-chatter/

    Unfortunately, I do not know javascript so I don't know how to make these tweaks so maybe you could help with these small improvements:

    1. Is there a way to link both the avatar and the text of the name of the person tweeting to that person's Twitter page?

    2. Can you link any text that begins with "@" to that person's twitter page?

    3. Link any hashtags to search.twitter with the results of that query.

    These requests are all basic things that search.twitter.com can do when displaying tweets. Your help would be greatly appreciated!

    -John
    http://twitter.com/infidelsarecool
    http://infidelsarecool.com
  • Have to appoligize. My net connection is messed up so I'm tethered to my iPhone. The software is not payed for so it only lets me do HTTP connections. Meaning no ftp or email. Can't browse to gmail either! Very weird. So here is the code snipped to add linked up name and image of poster:

    HTML += '<li id="TwitterId'+ result.id +'"><img src="' + result['profile_image_url']+'" alt="Profile Image" /> '+ result.from_user + '' + FormatIt(result.text) + '
    ' + relative_time(result.created_at) + ' via ' + linkup(result.source) + '</li>';

    Just replace the old HTML += with the code above!
  • Yes sir, I will work on this after the weekend is up. Will put the new version up here hopefully Monday or Tuesday! Thanks for the comments.
  • Hey - Came across this Great script -
    Many thanks

    I I will definateky let people know about it - Will digg it for sure

    Hamant
  • how would you make the user linkable to their twitter profile?
  • Ammy
    You could look for "@characters" string using javascript regex and replace with link to http://twitter.com/characters
  • xea
    Thanks very much for the relevant information! ;)
  • This is too confusing for me. Wish I knew more about computers.
  • bobo
    works like a charm :)
    thx
  • Having a few problems implementing it myself. Is it (or should it be) simple enough to drop into a wordpress blog page... maybe im missing something. Excited to see this work though, help!
  • I'm not sure how it works out with a wordpress theme. Basically what you need to do is include the latest jquery version in you hearder as well as the javascript code that i wrote. Then you need to put a class on the ul and li, and in return write them into the javascript code as well... Maybe I should re write the code so it isn't so general, and can be thrown into any template.. I'll look into it tonight.
  • Great! Thx, this is great work and seems simple enough to implement for the non-coders out there. And allows for total CSS revamp!
blog comments powered by Disqus