There are plenty of server-side RSS feed viewers out there but very little in the case of client-side JavaScript based viewers. Below I will go through the steps of creating a simple JavaScript based RSS feed viewer. Please note that this will only work with RSS feeds on the same domain. JavaScript does not allow cross domain scripting. You may find ways round this by using some of the Google API.
First begin by creating a HTML div container with a unique ID.
<div id="myDiv"></div>
Make a XML HTTP Request
The following function returns the data from an RSS (XML) page. As far as I am aware there is no way to use this cross domain, so you will have to look for a server-side script to work cross domains.
function httpGet(theUrl) { var xmlHttp = null; xmlHttp = new XMLHttpRequest(); xmlHttp.open("GET", theUrl, false); xmlHttp.send(); return xmlHttp.responseXML; } var rssFeedData = httpGet('http://www.tonyishere.co.uk/RSSExample/rss.xml');
Loop through the data and retrieve tags by name
Once this data has been stored in the variable as an object, we can use the “getElementsByTagName” function to pull out a particular tag (title in this case). Looping through all the tags called “title” will go through all of the XML from top to bottom. While looping through the tags, we can store the title and description in arrays to use later.
var i=0; var allTitles = []; var allDescriptions = []; // loop through all of the items and put them in arrays while (rssFeedData.getElementsByTagName("title")[i]) { allTitles[i] = rssFeedData.getElementsByTagName("title")[i]; allDescriptions[i] = rssFeedData.getElementsByTagName("description")[i]; flag=i; i=i+1; }
Loop through the arrays and render the data
Looking at the structure of the XML, you should be able to pick out the child nodes. In this case, each item is the first node (numbering starts from 0).
This can then be rendered as HTML and inserted into the div created earlier. In the example below I have decided to store the description and call an onclick function to show the description.
for (i=0;i<flag;i++){ titles[i]=allTitles[i].childNodes[0].nodeValue; descriptions[i]=allDescriptions[i].childNodes[0].nodeValue; document.getElementById('myDiv').innerHTML = newHTML; newHTML = document.getElementById('myDiv').innerHTML; newHTML = newHTML + "<div class=\"titlearea\" id=\"title" + i + "\" onclick=\"showdesc('" + i + "');\">" + titles[i] +"</div><div id=\"desc" + i + "\"></div>"; document.getElementById('myDiv').innerHTML = newHTML; }
Show the description
When the onclick function to display the description is run, the function below inserts the description stored in the array into the desc div. The array position was called as an argument to the function. This enables us to display the correct description in the div.
function showdesc(idc){ var idcdesc = "desc" + idc; document.getElementById(idcdesc).innerHTML = descriptions[(idc-1)]; }
Starting point for RSS feed viewer
Click on the link below to see the full code working. This is a very simple example of what can be done with RSS feeds using client-side scripting only. Hope this is of interest, please contact me on twitter for feedback and questions.