Archive for May, 2009

I fetched my twitter posts with jQuery, you can too!

without comments

I wanted to spice up my theme a bit with my latest twitter post. I also didn’t want to write any php to do it because I was feeling like writing some javascript instead. I also didn’t have a whole lot of time with the NHL playoffs on and all. This is where jQuery and twitter’s JSON api came to the rescue.

You can get your latest N posts from twitter from the following REST uri:

http://twitter.com/statuses/user_timeline/USERNAME.json?count=N

The api also supports JSONP callbacks which means that you can do a cross domain AJAX request without any problems. This is cool, because otherwise you would have to proxy it which means I would likely have to write some PHP and I didn’t want to do that ;)

Anyway, on to the jQuery:

1
2
3
4
5
6
$(document).ready(function() {
    $.getJSON("http://twitter.com/statuses/user_timeline/stephenminded.json?count=1&callback=?",
      function(data) {
        $("#tweets").prepend('<p>'+data[0].text+'</p>');
      });
  });

So, this makes a call to the twitter API asking for my latest tweet, once it gets that data it runs a callback which takes the tweet text and appends it in a paragraph tag to a div somewhere in my content (hint, it’s in the upper right corner). Oh, that “callback=?” part is the JSONP stuff, if that’s not there the request won’t work because of cross domain problems.

And with that I go back to watching the third period.

Written by steve

May 12th, 2009 at 10:54 pm

Posted in code, javascript, jquery

Tagged with ,