Retrieve tweets using jQuery

Twitter LogoHey all,

As many of you out there, I’m a Twitter user too. As most of you have, I too have spent quite some time reading the Twitter API and understanding it.

I stumbled upon a forum message (in Hebrew) asking how can you retrieve your (or anybody else’s) tweets using JavaScript only and no server side.

Well, there’s a way to accomplish that and it is not that complicated at all.

Change the twitterUsername var in the code to fit your username or the username you want to retrieve, please notice I took the “text” property only but the jSON result returns many more like:

Application used, profile image, time and more.

Here’s the code, enjoy!

   1: function getuserTweets() {

   2:     var twitterUsername = 'KensoDev';

   3:     var twitCount = 50;

   4:     $.getJSON(

   5:         '' + twitCount + '&q=from:' + twitterUsername,

   6:         function(data) {

   7:             $.each(data, function(i, userTweets) {

   8:                 for (var tweet = 0; tweet < userTweets.length; tweet++) {

   9:                     if (userTweets[tweet].text !== undefined) {

  10:                         $('body').append('<p>' + userTweets[tweet].text + '</p>');

  11:                     }

  12:                 }

  13:             });

  14:         }

  15:     );

  16: }

You can of course change the tags being created, you can change the object the tags are being appended into, you can change the code to better suit your needs that’s for sure.

If you need any help feel free to comment on this post and I will help you as much as I can.

Submit to Hacker News

[NOTE] This post has been imported to Hugo from a Wordpress blog. It may be broken, missing images, code snippets or other information.

[NOTE] updated September 27, 2009. It's 3410 days old . This article may have outdated content.