• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 546
  • Last Modified:

How do I make RSS feed in Widget display in revers chronological order?

I created a widget based upon Javascript code I examined in a pre-existing widget. (I am something of a newbie and just have a basic understanding of Javascript.) The widget reads an RSS feed from a Discussion Forum located on a secure server. When I look at the feed results using the RSS Reader in IE7, the results are in reverse chronological order (newest to oldest) like I want. However, when I view the source, the results are oldest to newest. I need to know how to programmatically reverse the order so that the results show up in my widget from newest to oldest.

Below are snippets from my HTML code and my Javascript code as well as a screenshot of the widget. I had to block out some stuff because this comes from a secure environment.
<title>Director's Blog Widget</title>
    <script src="script1.js"></script>
        body {font:8pt arial;margin:0;}
        #widgetBody {position:relative;background:url(bg.png) no-repeat;width:240px;height:299px;}
        #textWindowBox {position:absolute;top:75px;left:15px;width:200px;height:221px;overflow:hidden}
        #textWindow {position:relative;padding-top:7px;}
        #scrollBarBox {position:absolute;width:12px;height:216px;top:74px;left:221px;}
        #scrollBar {position:relative;width:12px;height:40px;background:url(scroll.jpg) no-repeat;}
        #ahome {position:absolute;top:52px;left:150px;width:75px;height:14px;cursor:pointer;background-color:transparent;}
        #home a {display:block;width:75px;height:14px;}
        .itemBlock {margin:0 0 3px 0;padding:0 0 4px 0;border-bottom:1px dotted #adafb3;}
        .itemDate {color:#666;font-size:.9em;line-height:1.1em;}
        .itemTitle {font-weight:bold;line-height:1.1em;}
        .itemTitle a {text-decoration:none;color:#254a7d;}
        .itemDescription {}
<body onLoad="init()">
<div id="widgetBody">
	<div id="home"><a href="url" target="_blank"></a></div>
	<div id="textWindowBox">
		<div id="textWindow">
			<div id="outputBlock"></div>
	<div id="scrollBarBox">
		<div id="scrollBar" class="dragme"></div>
	<div style="clear:both;"></div>
<script language="javaScript">getData(2)</script>
var http_request = false;
var dataFileName;
dataFileName = "rss source from Discussion Forum";
function getData(dataFileIndex) {
	if (window.ActiveXObject) { //IE
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) { //other
		http_request = new XMLHttpRequest();
	} else {
		alert("your browser does not support AJAX");
	http_request.setRequestHeader("Cache-Control", "no-cache");
	http_request.setRequestHeader("Pragma", "no-cache");
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				if (http_request.responseText != null) {
				} else {
					alert("Failed to receive RSS file from the server - file not found.");
					return false;
function processRSS(rssxml) {
	RSS = new RSS2Channel(rssxml);
function RSS2Channel(rssxml) {
	this.items = new Array();
	var itemElements = rssxml.getElementsByTagName("item");
	for (var i=0; i<itemElements.length; i++) {
		Item = new RSS2Item(itemElements[i]);
function RSS2Item(itemxml) {
    var properties = new Array("title", "link", "description", "pubDate", "channel");
    var tmpElement = null;
    for (var i=0; i<properties.length; i++) {
        tmpElement = itemxml.getElementsByTagName(properties[i])[0];
        if (tmpElement != null) {
function outputData(RSS) {
	dataString = "";
    for (var i=0; i<RSS.items.length; i++) {
		dataString += "<div class='itemBlock'>";
		newDate = new Date(RSS.items[i].pubDate);
		dateString = (newDate.getMonth()+1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
		dataString += "<div class='itemDate'>" + dateString + "</div>";
		dataString += "<div class='itemTitle'><a href='" + RSS.items[i].link + "' target='afps_news'>" + RSS.items[i].title + "</a></div>";
		//dataString += "<div class='itemDescription'>" + RSS.items[i].description + "</div>";
		dataString += "</div>";
	document.getElementById('outputBlock').innerHTML = dataString;
function compFunc (a, b)
 	if (a.pubDate < b.pubDate)
  		return 1;
 	else if (a.pubDate > b.pubDate)
  		return -1;
  		return 0;
var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;
var scrollPercent;
var boxTop;
var maxHeight;
var toppoint;
var scrollBarBoxOffset = 74;
function init() {
	document.getElementById('scrollBarBox').style.top = "74px";
	document.getElementById('scrollBarBox').style.height = "216px";
	document.getElementById('scrollBar').style.height = "40px";
function movemouse(e) {
	if (isdrag) {
		//dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
		toppoint = (nn6) ? ty + e.clientY - y : ty + event.clientY - y;
		boxTop = parseInt(document.getElementById('scrollBarBox').style.top) - scrollBarBoxOffset;
		if (toppoint < boxTop) toppoint = boxTop;
		boxHeight = parseInt(document.getElementById('scrollBarBox').style.height);
		maxHeight = boxTop + boxHeight - parseInt(document.getElementById('scrollBar').style.height);
		if (toppoint > maxHeight) toppoint = maxHeight;
		dobj.style.top = toppoint + "px";
		scrollPercent = toppoint / maxHeight;
		document.getElementById('textWindow').style.top = parseInt(0 - (document.getElementById('textWindow').offsetHeight - parseInt(document.getElementById('scrollBarBox').style.height)) * scrollPercent );
		return false;
function selectmouse(e) {
	var fobj = nn6 ? e.target : event.srcElement;
	var topelement = nn6 ? "HTML" : "BODY";
	while (fobj.tagName != topelement && fobj.className != "dragme") {
		fobj = nn6 ? fobj.parentNode : fobj.parentElement;
	if (fobj.className == "dragme") {
		isdrag = true;
		dobj = fobj;
		//tx = parseInt(dobj.style.left + 0);
		ty = parseInt(dobj.style.top + 0);
		//x = nn6 ? e.clientX : event.clientX;
		y = nn6 ? e.clientY : event.clientY;
		document.onmousemove = movemouse;
		return false;
document.onmousedown = selectmouse;
document.onmouseup = new Function("isdrag=false;");

Open in new window

  • 3
  • 3
1 Solution
        for (var i=itemElement.length; i>0; i--) {
                Item = new RSS2Item(itemElements[i]);
kimberleyharrisAuthor Commented:
Thank you bluV11t, but that does not seem to be working. I am adding the XML structure just to be sure I haven't made a mistake in the way I am coding to it. I have a feeling I need a function for the <pubDate> but I am not sure. I also am not sure how to approach it.
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="rss.xsl" media="screen"?><rss version="2.0">
			<pubDate>Wed, 05 Sep 2007 13:28:56 GMT</pubDate>

Open in new window

Maybe I wasn't beeing very clear... my suggestion is to change the function RSS2Channel to reverse the for-loop like this:

function RSS2Channel(rssxml) {
        this.items = new Array();
        var itemElements = rssxml.getElementsByTagName("item");
        for (var i=itemElement.length; i>0; i--) {
                Item = new RSS2Item(itemElements[i]);

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

kimberleyharrisAuthor Commented:
Yes, I understood. I did it, but it still did not work. Not sure why.
Sorry, my mistake :-) Try:

        for (var i=itemElements.length; i>0; i--) {
                Item = new RSS2Item(itemElements[i-1]);
kimberleyharrisAuthor Commented:
Awesome! That is exactly what I needed! Thank you so much for your help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now