How to work with RSS using Google Feed API and JavaScript

You or your client's website might require displaying links to current events and news from local, national, or international sources. With Really Simple Syndication also known as Rich Site Summary (RSS), you can provide linking to recent updates from websites that provide the RSS service. Typically most Internet users that receive RSS feeds utilize an RSS reader or news aggregator to keep up to date on their favorite websites or news resources, either from within their desktop, web-based device, or mobile-based device. This series will demonstrate several approaches in which you can present RSS feeds from one source or more using simple JavaScript and the Google Feed API method. Browser support for the Google Feed API currently includes Chrome, Firefox 1.5+, IE6+, Opera 9+, and Safari.

One RSS Feed

Consider adding an RSS feed to your website using this simple model for the Google Feed API, the instructions include explanations of the API's configurable JavaScript components.

Load the JavaScript API

One of the great things about using the Goggle Feed API method is that an API key is no longer required. If you have previously set up a Google Feed API method, you can now remove the call to your API key; however, it should continue to work just fine if you decide to keep the API key in your script. Just strip out the key equals section of the script as shown in the examples below. The second snippet below is added to the header of your web page.

Next, you will need to load the Feed API into the header of your web page with the google.load(module, version, package) where the module calls the specific API you wish to use on your page. In this case the version is the specific version number you wish to load ( 1), and the package indicates the specific elements package you wish to load, which in this case, is Feed. The example script Feed API is displayed in the snippet below.

If you are concerned with the version number getting updated or compliance, you can always check the Google AJAX API Discussion Group. Notifications of any version updates will be posted there along with any code changes that might require you to update your URLs. The nice think about the Google API is that their team periodically makes bug fixes and updates that do not require a version number change, and these remain mostly transparent; however, should you ever find that your API client is not working you may need to review or report any issues using the API discussion group.

Using the API

Next, we need to add the Google Feed API to the body of our web page, and this requires an asynchronous call to a server with a callback to exchange the data between the feed URL (through loading the feed using feedurl) and RSS output (rssoutput variable function). The next code snippet is modified and inspired by JavaScript Kit, in particular the DHTML and DOM reference to RSS feeds.

The first section of the script sets the variables ‘var' with the feed container getting the element by id equals feed. Then, the feed URL is declared and the feed limit is set to 10; the RSS output is set to a second level heading and starts an unordered list as displayed below. In this example, I will use the NPR Business News RSS feed URL of http://www.npr.org/rss/rss.php?id=1006 and set it to pull the last ten feeds, which is just one of over fifty NPR RSS Feeds.

<script type="text/javascript">

var feedcontainer=document.getElementById("feed")

var feedurl="http://www.npr.org/rss/rss.php?id=1006"

var feedlimit=10

var rssoutput="<h2>Latest NPR Business News:</h2><ul>"

The next section sets the function RSS feed setup (rssfeedsetup() ) with the variable feed pointer set to Google feeds (google.feeds.Feed(feedurl) ), the feed pointer set to the number of entries (feedpointer.setNumEntries(feedlimit) ), and then it loads the feed display (feedpointer.load(displayfeed) ).

function rssfeedsetup(){

var feedpointer=new google.feeds.Feed(feedurl)

feedpointer.setNumEntries(feedlimit)

feedpointer.load(displayfeed)

}

If there is an error displaying any of the RSS feed the JavaScript alert message, "Error fetching feeds!" gets displayed.

Related Topics:

About Ryan Boudreaux

Ryan has performed in a broad range of technology support roles for electric-generation utilities, including nuclear power plants, and for the telecommunications industry. He has worked in web development for the restaurant industry and the Federal g...

Full Bio

Ryan has performed in a broad range of technology support roles for electric-generation utilities, including nuclear power plants, and for the telecommunications industry. He has worked in web development for the restaurant industry and the Federal government.