Created
December 3, 2012 00:42
-
-
Save steinbring/4191852 to your computer and use it in GitHub Desktop.
How to consume and display RSS in JavaScript
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
<!DOCTYPE html> | |
<html lang="en-US"> | |
<head> | |
<title>How to display RSS via JavaScript</title> | |
<!-- jQuery CDN --> | |
<script src="http://code.jquery.com/jquery-latest.js"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
function getRSSFeed(feed){ | |
// Build the YQL query | |
var qryRSS = 'select * from rss where url='+'"'+feed+'"'; | |
// Initiate the YQL query | |
$.getJSON("http://query.yahooapis.com/v1/public/yql", | |
{ | |
// These are the settings for your API call | |
q: qryRSS, | |
format: "json" | |
}, | |
// Take the data you got from the YQL server and output it to the screen. The variable "data" holds the JSON you got back from the YQL server. | |
function(data) { | |
// Just bother with the last 4 entries within the JSON feed | |
for (i=0; i<4; i++) | |
{ | |
// Output a link, using the link attribute and the title attribute | |
$('body').append('<a href="'+data.query.results.item[i].link+'">'+data.query.results.item[i].title+'</a><br />'); | |
// Output the description, using the description attribute. | |
$('body').append(data.query.results.item[i].description); | |
} | |
}); | |
}; | |
// Test it with Steinbring.net's RSS Feed | |
getRSSFeed('http://steinbring.net/feed/'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment