Created
December 8, 2013 07:09
-
-
Save ahmad24/7854178 to your computer and use it in GitHub Desktop.
jQuery : load tweets ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
// When the DOM is ready, add behavior to the link | |
$(document).ready(function(){ | |
$('#load').click(function(){ | |
load_tweets(); | |
// Skip default behavior (ie redirecting to the link href) | |
return false; | |
}); | |
}); | |
// Main function: load tweets in JSON | |
function load_tweets() { | |
// Activity indicator: | |
$('#tweets').html('loading tweets...'); | |
// Ajax JSON request | |
$.getJSON( | |
// Use a JSONP (with callback) URL | |
'http://twitter.com/status/user_timeline/ozh.json?count=5 & callback=?', | |
// Function that will handle the JSON response | |
function(data) { | |
// Put empty <ul> in the placeholder | |
$('#tweets').html('<ul></ul>'); | |
// Read each object in the JSON response | |
$(data).each(function(i, tweet) { | |
$('#tweets ul').append('<li>'+tweet.text+'</li>'); | |
}); | |
} | |
); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment