javascript问题,rss feed显示

时间:2009-10-08 22:13:19

标签: javascript html ajax rss xmlhttprequest

我的代码将rss Feed中的项目列在html页面上。虽然,java脚本有点挑剔。它不会读取一些xml提要,通常是包含超过25个列表项的提要。我只需要另一组眼睛来查看代码并告诉我是否遗漏了一些明显的东西。

.js file-----------------------------------------------
//XML CODE

var http_request = false;
var dataFileName = new Array();
dataFileName[1] = "http://newsrss.bbc.co.uk/rss/newsonline_world_edition/americas/rss.xml";
//dataFileName[2] = "http://newsrss.bbc.co.uk/rss/newsonline_world_edition/uk_news/magazine/rss.xml";
//dataFileName[3] = "http://newsrss.bbc.co.uk/rss/newsonline_world_edition/business/rss.xml";




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.open("GET",dataFileName[dataFileIndex],true);
    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) {
                    processRSS(http_request.responseXML);
                } else {
                    alert("Failed to receive RSS file from the server - file not found.");
                    return false;
                }
            }
        }
    }

    http_request.send(null);
}

function processRSS(rssxml) {
    RSS = new RSS2Channel(rssxml);
    outputData(RSS);
}

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]);
        this.items.push(Item);
    }
}

function RSS2Item(itemxml) {
    this.title;
    this.link;
    this.description;
    this.pubDate;
    this.guid;

    var properties = new Array("title", "link", "description", "pubDate", "guid");
    var tmpElement = null;
    for (var i=0; i<properties.length; i++) {
        tmpElement = itemxml.getElementsByTagName(properties[i])[0];
        if (tmpElement != null) {
            eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
        }
    }
}

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;
}





//SCROLL BAR CODE

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;

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;");

html file-------------------------------------------------------------------

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD><TITLE>TEST</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<SCRIPT src="script1.js"></SCRIPT>

<STYLE>BODY {
    MARGIN: 0px; FONT: 8pt arial
}
#widgetBody {
BACKGROUND-Color:gray;  WIDTH: 240px; POSITION: relative; HEIGHT: 299px
}
#textWindowBox {
    LEFT: 63px; OVERFLOW: hidden; WIDTH: 152px; POSITION: absolute; TOP: 70px; HEIGHT: 221px
}
#textWindow {
    PADDING-TOP: 7px; POSITION: relative
}
#scrollBarBox {
    LEFT: 221px; WIDTH: 12px; POSITION: absolute; TOP: 74px; HEIGHT: 216px
}
#scrollBar {
    BACKGROUND: url(images/widget_scroll-handle1.gif) no-repeat; WIDTH: 12px; POSITION: relative; HEIGHT: 40px
}
#defenseLinkLink {
    LEFT: 4px; WIDTH: 20px; CURSOR: pointer; POSITION: absolute; TOP: 155px; HEIGHT: 140px; BACKGROUND-COLOR: transparent
}
#defenseLinkLink A {
    DISPLAY: block; WIDTH: 20px; HEIGHT: 140px
}
.dragme {
    POSITION: relative
}
.itemBlock {
    PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 4px; MARGIN: 0px 0px 3px; PADDING-TOP: 0px; BORDER-BOTTOM: #adafb3 1px dotted
}
.itemDate {
    FONT-SIZE: 0.9em; COLOR: #666; LINE-HEIGHT: 1.1em
}
.itemTitle {
    FONT-WEIGHT: bold; LINE-HEIGHT: 1.1em
}
.itemTitle A {
    COLOR: #254a7d; TEXT-DECORATION: none
}
.itemDescription {

}
</STYLE>

<SCRIPT>
var scrollBarBoxOffset = 74;

function init() {
    document.getElementById('scrollBarBox').style.top = "74px";
    document.getElementById('scrollBarBox').style.height = "216px";
    document.getElementById('scrollBar').style.height = "40px";
}
</SCRIPT>

<META content="MSHTML 6.00.6001.18294" name=GENERATOR></HEAD>
<BODY onload=init()>
<DIV id=widgetBody>
<DIV id=textWindowBox>
<DIV id=textWindow>
<DIV id=outputBlock></DIV></DIV></DIV>
<DIV id=scrollBarBox>
<DIV class=dragme id=scrollBar></DIV></DIV>
<DIV style="CLEAR: both"></DIV></DIV>
<SCRIPT language=javaScript>getData(2)</SCRIPT>
</BODY></HTML>

3 个答案:

答案 0 :(得分:0)

哦,伙计,你为什么直接使用XMLHttpRequest?使用一个库,让你的生活更轻松:)

您可能遇到跨站点脚本安全问题。如果RSS源存在于与运行JavaScript的页面不同的域中,则浏览器将不允许您的JavaScript发出请求。

答案 1 :(得分:0)

  

dataFileName [1] =“http://newsrss.bbc.co.uk/rss/newsonline_world_edition/americas/rss.xml”;

除非你是(a)从BBC运行的脚本,或(b)浏览器扩展,否则你不能向该服务器发出XMLHttpRequest。

  dataString += "<div class='itemTitle'><a href='" + RSS.items[i].link

HTML /脚本注入。如果您坚持使用innerHTML而不是使用简单的DOM方法,则必须执行HTML转义才能将<&"转换为&lt;&amp;&quot;

  

的eval( “这一点。” +属性[I] + “= tmpElement.childNodes [0] .nodeValue”);

请勿使用eval,除非您需要它的极少数异常情况。这不是其中之一;您可以使用以下命令按名称访问JavaScript属性:

this[properties[i]]= tmpElement.firstChild.data;

此外,这可能是不可靠性的来源,您无法确定是否会有一个子Text节点。如果该元素中没有内容,则firstChild / childNodes[0]将不存在,您将获得异常。如果元素中存在复杂的内容(通常不应该存在,但对于RSS 0.9,可能存在未编码的HTML),firstChild.nodeValue将不包含元素的文本内容。相反,您必须遍历收集其nodeValue / data的文本节点后代。

答案 2 :(得分:0)

好的,让它运转起来。 2期。

  1. army.mil无法解决!请改用“www.army.mil”。

  2. 在RSS2Item中替换此行: if(tmpElement!= null){

  3. 用这个: if(tmpElement!= null&amp;&amp; tmpElement.childNodes [0]){