RSS and AJAX: A Simple News Reader
by Paul Sobocinski
|
Pages: 1, 2, 3, 4, 5
Displaying the RSS Data: The showRSS(RSS) Function
Before we go into the JavaScript code for the showRSS(RSS) function, let's have a look at the root div element of the HTML page mentioned earlier:
<div class="rss" id="chan">
<div class="rss" id="chan_title"></div>
<div class="rss" id="chan_link"></div>
<div class="rss" id="chan_description"></div>
<a class="rss" id="chan_image_link" href=""></a>
<div class="rss" id="chan_items"></div>
<div class="rss" id="chan_pubDate"></div>
<div class="rss" id="chan_copyright"></div>
</div>
As you can see, the root div element has a number of child div tags. These tags will be populated with the data in the RSS object by the showRSS(RSS) function, which is shown below.
function showRSS(RSS)
{
/*A*/
var imageTag = "<img id='chan_image'";
var startItemTag = "<div id='item'>";
var startTitle = "<div id='item_title'>";
var startLink = "<div id='item_link'>";
var startDescription = "<div id='item_description'>";
var endTag = "</div>";
/*B*/
var properties = new Array("title","link","description","pubDate","copyright");
for (var i=0; i<properties.length; i++)
{
eval("document.getElementById('chan_"+properties[i]+"').innerHTML = ''"); /*B1*/
curProp = eval("RSS."+properties[i]);
if (curProp != null)
eval("document.getElementById('chan_"+properties[i]+"').innerHTML = curProp"); /*B2*/
}
/*C*/
/*show the image*/
document.getElementById("chan_image_link").innerHTML = "";
if (RSS.image.src != null)
{
document.getElementById("chan_image_link").href = RSS.image.link; /*C1*/
document.getElementById("chan_image_link").innerHTML = imageTag
+" alt='"+RSS.image.description
+"' width='"+RSS.image.width
+"' height='"+RSS.image.height
+"' src='"+RSS.image.url
+"' "+"/>"; /*C2*/
}
/*D*/
document.getElementById("chan_items").innerHTML = "";
for (var i=0; i<RSS.items.length; i++)
{
item_html = startItemTag;
item_html += (RSS.items[i].title == null) ? "" : startTitle + RSS.items[i].title + endTag;
item_html += (RSS.items[i].link == null) ? "" : startLink + RSS.items[i].link + endTag;
item_html += (RSS.items[i].description == null) ? "" : startDescription + RSS.items[i].description + endTag;
item_html += endTag;
document.getElementById("chan_items").innerHTML += item_html; /*D1*/
}
return true;
}
A: As we have no way of knowing the number of channel items in the RSS feed, we must dynamically generate the HTML for the RSS items. These are the default values for the HTML tags that will contain the RSS2Item data. For compatibility, we also dynamically generate the img HTML tag.
B: We traverse the string properties in the RSS2Category object here, similar to how we did in the constructor. In order to clear any data that may remain from an old RSS feed, we reset the innerHTML property on line B1. We are able to fetch the specific div element that we need from the HTML by calling getElementById(). Providing that the property is defined, we set the div element to its new value on line B2.
C: Again, we use the getElementById() function to get the HTML element that will contain the image from the RSS feed. As the image should be linkable, we use an anchor element (a) instead of a div element. The href attribute in the anchor element specifies what the image should link to, so we assign it to the value found in RSS.image.link (C1). The content of the element is filled in using the innerHTML property, as we have done in part B (C2).
D: Here is where we display the items in the RSS object. A div tag is defined for each RSS item, containing the title, link, and description. For the sake of clarity, the other properties have been omitted. Each div tag is appended to the contents of the chan_items parent tag using the innerHTML property (D1).
Wrap-Up
The Ajax RSS parser has been tested in IE 6.0 and Firefox 1.5.0.6 for Windows XP. The RSS2Channel object does not support all of the elements in the RSS 2.0 specification. The ones that have been omitted are cloud, textInput, skipHours, and skipDays. For the most part, these RSS elements are only useful on the server side, so it wouldn't make sense to include them in a client-side parser.
After noting the length of the code, you may be thinking that the same functionality could have been accomplished with half the number of lines of code. In particular, we could have completely omitted the RSS object by writing the showRSS(RSS) function in a way that reads the RSS properties directly from the XML element. Certainly, this is possible. However, showRSS() is only meant to be an example of how the RSS2Channel object can be used. By defining an RSS object that contains meaningful RSS data, we have a much more scalable application. For example, the code can be easily extended to fetch multiple feeds. The RSS objects from these feeds can then be manipulated, or compared with other feeds (you can fetch a new feed after a certain interval, and compare it with the old one). The point of a separate RSS object is to make increasingly complex applications like this easier to develop.
All of the files that were discussed are available below:
The HTML file: rssajax.html
The JavaScript file, containing the RSS parser: rssajax.js
Sample RSS file 1: test-rss.xml
Sample RSS file 2: google-rss.xml
- Parsing live feeds
2009-04-21 11:25:36 YoonMi - local files works but remote not.
2009-04-05 05:36:24 Olijf - Cant get it to work on Firefox 2
2008-07-08 18:38:44 ray888 - Items with different amount of properties?
2008-03-08 09:01:58 johangoras - Can not fetch from remote server
2007-12-17 05:16:46 or_gust - the XML pages are not shown when clicked
2007-08-02 09:27:28 cumap - the XML pages are not shown when clicked
2007-08-02 09:47:23 cumap - WIll this work with direct URL submission...
2007-06-15 13:18:17 Amanjeev - WIll this work with direct URL submission...
2007-06-27 06:54:11 agb81 - WIll this work with direct URL submission...
2007-06-27 07:21:22 sobes - WIll this work with direct URL submission...
2008-03-15 02:23:59 Arvindpatel - outside link, not local
2007-05-30 08:34:28 FJBrian - outside link, not local
2007-06-15 13:23:41 Amanjeev - outside link, not local
2007-06-27 07:23:44 sobes - outside link, not local
2009-10-21 07:16:04 Levitikon - Refresh Problems
2007-03-05 15:44:21 martha123 - Refresh Problems
2007-03-16 21:23:18 sobes - JavaScript - Error Code 0 Received
2007-01-31 06:53:56 UIGuy - JavaScript - Error Code 0 Received
2007-01-31 08:58:07 UIGuy - JavaScript - Error Code 0 Received
2007-01-31 06:55:14 UIGuy - JavaScript - Error Code 0 Received
2007-01-31 10:04:10 sobes - JavaScript - Error Code 0 Received
2007-06-06 06:56:41 Wildbill - This code doesn`t work in Opera
2006-10-14 12:10:13 Rudnytskiy - eval() unnecessary
2006-09-17 05:29:48 happygiraffe - Excellent work,but..
2006-09-14 07:13:13 Eki - Excellent work,but..
2006-09-15 07:00:38 sobes - Excellent work,but..
2006-09-15 10:14:47 Eki - Poor JavaScript quality
2006-09-14 02:36:03 Asbjørn Ulsberg - Poor JavaScript quality
2007-02-24 09:28:12 kahunabear - Poor JavaScript quality
2006-09-15 07:31:43 sobes - Poor JavaScript quality
2006-09-14 23:01:33 allanc@chickenandporn.com - Developing the code
2006-09-13 23:57:15 StuartTurner