Displaying RSS feeds easily using Google Ajax Feed APIDisplaying RSS feeds from other sites (ie: CNN or Digg) on your own is a nice way to show constantly updated content automatically. However, for the novice web developer, setting this up can be daunting, requiring both knowledge from you and your server's ability to host remote files. Google's Ajax Feed API changes all that, by basically enabling any webmaster to display RSS feeds on their sites using some JavaScript code. It hosts the desired RSS feeds on their servers for you, caches them, and returns the data in either JSON or XML format for you to utilize. All that's left is for you to use some JavaScript to parse this data and output it on your page.
In this tutorial, I'll show you how to harness Google Ajax Feed API to fetch a RSS feed and display it on your site.
Let's do a simple one:
index.html
<head>
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAA0e8RGYZwE5URQWc4oWJ3phSyNZmDXr_GxMr9-NzSzWzH7atsohQVQ1v1TrWuygNorMyOX33Ai5fAqw">
</script>
<script type="text/javascript" src="rssdisplayer.js"></script>
<style type="text/css">
#msndiv{width: 500px;}
</style>
</head>
<body>
<hr>
<script type="text/javascript">
//USAGE SYNTAX: new rssdisplayer("divid", "rssurl", numberofitems, "displayoptions")
new rssdisplayer("msndiv", "http://www.msnbc.msn.com/id/3032091/device/rss/rss.xml", 6, "date, description")
</script>
<hr>
rssdisplayer.js
google.load("feeds", "1") //Load Google Ajax Feed API (version 1)
function rssdisplayer(divid, url, feedlimit, showoptions){
this.showoptions=showoptions || "" //get string of options to show ("date" and/or "description")
var feedpointer=new google.feeds.Feed(url) //create new instance of Google Ajax Feed API
feedpointer.setNumEntries(feedlimit) //set number of items to display
document.write('<div id="'+divid+'">Loading feed, </div>')
this.feedcontainer=document.getElementById(divid)
var displayer=this
feedpointer.load(function(r){displayer.formatoutput(r)}) //call Feed.load() to retrieve and output RSS feed
}
rssdisplayer.prototype.formatdate=function(datestr){
var itemdate=new Date(datestr)
return "<span style='color:gray; font-size: 90%'>"+itemdate.toLocaleString()+"</span>"
}
rssdisplayer.prototype.formatoutput=function(result){
if (!result.error){ //if RSS feed successfully fetched
var thefeeds=result.feed.entries //get all feed entries as a JSON array
var rssoutput="<ul>"
for (var i=0; i<thefeeds.length; i++){ //loop through entries
var itemtitle="<a href=\"" + thefeeds[i].link + "\">" + thefeeds[i].title + "</a>"
var itemdate=/date/i.test(this.showoptions)? this.formatdate(thefeeds[i].publishedDate) : ""
var itemdescription=/description/i.test(this.showoptions)? "<br />"+thefeeds[i].content : /snippet/i.test(this.showoptions)? "<br />"+thefeeds[i].contentSnippet : ""
rssoutput+="<li>" + itemtitle + " " + itemdate + itemdescription + "</li>"
}
rssoutput+="</ul>"
this.feedcontainer.innerHTML=rssoutput
}
else //else, output error
alert("Error fetching feeds: "+result.error.message)
}
Get your own google key to replace the one i used. The result will be shown below.
I will not bother myself to explain how the rss class work. And by the way, do not mistaken that prototype u are seeing with the jquery prototype framework - this one here is the generic prototype we use in javascript to create classes.
A brief explanation is that - there are 2 scripts involves, our own rss display class, and the external google ajax engine.
To learn more about this technique, please follow this link:
http://www.javascriptkit.com/dhtmltutors/googleajaxfeed.shtmlAnything, you do not understand - just bring it here - i will explain as best as i can.