Main Tutorials

How to get last Tweet via jQuery

Twitter, provides many APIs to deal with Tweets. In this example we are going to learn how to use jQuery to get last tweet of a user.

Twitter API
Get the last tweet of user (change “username” in the url with Twitter username you want)


http://search.twitter.com/search.json?q=from:username&rpp=1

For example, get last tweet from twitter’s user “mkyong”.


http://search.twitter.com/search.json?q=from:mkyong&rpp=1

1. jQuery Ajax

We use Jquery .ajax() function to get the last tweet of the user and display it.


$('#last-tweet').click(function(){

   $("#tweet_result").text("Loading......");

   var username = $('#username').val();

   $.ajax({
     type: "GET",
     dataType: "json",
     url: "http://search.twitter.com/search.json?q=from:"+username+"&rpp=1&callback=?",
     success: function(data){
        $("#tweet_result").text(data.results[0].text);
     }
   });

});

2. Full HTML Example

In this example, write the username into the box and click on the button to get last tweet of the user…


<html>
<head>
<title>How to Get Last Tweet with Jquery Example</title>
<script
	src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
	type="text/javascript"></script>
</head>

<body>
	<h1>Get Last Tweet with jQuery</h1>

	username :
	<input type='text' id='username' size='50' value='mkyong' />
	<br />
	<br />
	<h2>
		Last Tweet : <span id="tweet_result"></span>
	</h2>

	<button id="last-tweet">Get Last Tweet (.Ajax)</button>
	<script type="text/javascript">
	$('#last-tweet').click(
	  function() {

		$("#tweet_result").text("Loading......");

		var username = $('#username').val();

		$.ajax({
			type : "GET",
			dataType : "json",
			url : "http://search.twitter.com/search.json?q=from:"
				+ username + "&rpp=1&callback=?",
			success : function(data) {
				$("#tweet_result").text(data.results[0].text);
			}
	  });
	});
	</script>

</body>
</html>

Try it yourself, type twitter username and click on the button.

Feel free to ask any question here…

Reference

  1. jQuery Ajax example

About Author

author image
Love Programming, open source stuff and hybrid cars. Follow him on Twitter and his Blog.

Comments

Subscribe
Notify of
18 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Jignesh Patel
10 years ago

I type hellojiger in the field and hit on get last tweet (.ajax) button it is not showing me any result. which API you are using ?

yuceltoluyag
10 years ago

not work

Neel
11 years ago

Mkyong,

Apologize for submitting data manytimes, because of cache problem in the browser, could not see the latest comments which i posted, so i had to submit many times. So please delete the duplicate comments which i posted (last 4) and this comment.

Neel
11 years ago

Used for loop to get the latest two tweets it worked for me.

success : function(data) {
for(var i=0; i<2;i++){
$("#tweet_result").text(data.results[i].text);
$("#tweet_link").append("“+”Click Here“);
}//for loop
}//success

to know the attributes present in dataResults array use dataResults[index].toSource();
and customize your code based on the requirements.

Neel
11 years ago

Used for loop to get the latest two tweets it worked for me.

success : function(data) {
  for(var i=0; i<2;i++){
    $("#tweet_result").text(data.results[i].text);
    $("#tweet_link").append("<a href="+"https://twitter.com/"+data.results[i].from_user+"/status/"+data.results[i].id_str+">"+"Click Here</a>");}//for loop
	}//success

to know the attributes present in dataResults array use dataResults[index].toSource();
and customize your code based on the requirements.

Neel
11 years ago

Used for loop to get the latest two tweets it worked for me.

success : function(data) {
  for(var i=0; i<2;i++){
    $("#tweet_result").text(data.results[i].text);
    $("#tweet_link").append("<a href="+"https://twitter.com/"+data.results[i].from_user+"/status/"+data.results[i].id_str+">"+"Click Here</a>");}//for loop
	}//success

to know the attributes present in dataResults array use dataResults[index].toSource();
and customize your code based on the requirements.

Neel
11 years ago

Hey used for loop to get the latest two tweets it worked for me.

success : function(data) {
  for(var i=0; i<2;i++){
    $("#tweet_result").text(data.results[i].text);
    $("#tweet_link").append("<a href="+"https://twitter.com/"+data.results[i].from_user+"/status/"+data.results[i].id_str+">"+"Click Here</a>");}//for loop
	}//success

to know the attributes present in dataResults array use dataResults[index].toSource();
and customize your code based on your requirements.

Neel
11 years ago

ayalcinkaya,

Thankyou for this article. It helped me a lot..

Working on to get the last two latest tweets from Twitter. Do you have any thoughts on this. Is there a way to get the last two tweets.

Neel
11 years ago
Reply to  Neel

Used for loop to get the latest two tweets it worked for me.

success : function(data) {
  for(var i=0; i<2;i++){
    $("#tweet_result").text(data.results[i].text);
    $("#tweet_link").append("<a href="+"https://twitter.com/"+data.results[i].from_user+"/status/"+data.results[i].id_str+">"+"Click Here</a>");}//for loop
	}//success

to know the attributes present in dataResults array use dataResults[index].toSource();
and customize your code based on the requirements.

Josh
11 years ago

The new Twitter API is doing away with this method, starting with blackout testing today. Any ideas on how to use the new API? The documentation is pretty opaque to me.

Jana
11 years ago

Remember, a number of the people come from a normal
income background, that is why they are hunting for things online.

For more information regarding our Internet marketing Services for
UK then kindly join us and send your proposal now.
Most often, bricks and mortar businesses spend far more than
$20 to market their products and services in local
newspapers, mail coupon campaigns, and other print advertising.

Pedro Felipe
11 years ago

Thanks!!!

Serge
11 years ago

Nice piece of code.
But how can you get the last tweet to be displayed as link, a sort of “linkified” version of what you already have now?

Thanks!

ayalcinkaya
11 years ago
Reply to  Serge

Hi Serge,

You can create the link yourself using tweet id and profile name. I could not test it but it must work.

$(“#tweet_result”).text(“https://twitter.com/”+data.results[0].from_user_name+”/status/”+data.results[0].id);

change this and it gives you the url of the tweet.

I hope I understand the right thing…

Yokai
11 years ago
Reply to  ayalcinkaya

Proper Code:

$("#tweet_link").text("https://twitter.com/"+data.results[0].from_user+"/status/"+data.results[0].id_str);

(Please note that this is to make the link show in addition to the already displayed text)

1. Changed id to id_str at the end
2. Changed from_user_name to from_user
3a. Changed #tweet_result to #tweet_link so you can display both the tweet content and the link
3b. Need to add

<span id="tweet_link"></span>

for the additional info to display.

-Yokai

Serge
11 years ago
Reply to  ayalcinkaya

ayalcinkaya,

Thanks for the help, meanwhile I went with the link-less version of it. Hopefully someone else will be able to use it soon enough.

Thanks again!

S.

Koray Tugay
11 years ago

I type fatabass in the field and click the get latest tweet but it does not work ?

ayalcinkaya
11 years ago
Reply to  Koray Tugay

fatabass’s account is protected. Twitter just giving public twitter acoount’s tweet…

Thank’s for reporting bug