天下第二博

Tian Xia The Second BO
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

javascript读到RSS

Posted on 2007-04-26 10:38  Nuke'Blog  阅读(200)  评论(0编辑  收藏  举报
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>RSS Reader</title>
<!--copyright Turkeycock-->
<script type="text/javascript">
var xmlHttp;

function ShowRSS() {
var target=document.getElementById("targeturl");
//alert(target.value);
readRSS(target.value);
}

function createXMLHttpRequest() {
    
if (window.ActiveXObject) {
        xmlHttp 
= new ActiveXObject("Microsoft.XMLHTTP");
    }
 
    
else if (window.XMLHttpRequest) {
        xmlHttp 
= new XMLHttpRequest();
    }

}

    
function readRSS(url) {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange 
= handleStateChange;
    xmlHttp.open(
"GET", url, true);
    xmlHttp.send(
null);
}

    
function handleStateChange() {
    
if(xmlHttp.readyState == 4{
        
if(xmlHttp.status == 200{
            clearPreviousResults();
            parseResults();
        }

    }

}


function clearPreviousResults() {
    
var ListBody = document.getElementById("resultsTitle");
    
while(ListBody.childNodes.length > 0{
        ListBody.removeChild(ListBody.childNodes[
0]);
    }

}


function parseResults() {
    
var results = xmlHttp.responseXML;
    
var title = null;
    
var content=null;
    
var item = null;

    
var items = results.getElementsByTagName("item");
    
for(var i = 0; i < items.length; i++{
        item 
= items[i];
        title 
= item.getElementsByTagName("title")[0].firstChild.nodeValue;
        content
=item.getElementsByTagName("description")[0].firstChild.nodeValue;
        addListRow(title,content);
        
    }


}



function addListRow(title,content) {
    
var row = document.createElement("div");
    
var cell = createCellWithText(title);
    
var contentcell=createCellWithContent(content);
    row.appendChild(cell);
    row.appendChild(contentcell);
    
    document.getElementById(
"resultsTitle").appendChild(row);
}


function createCellWithText(text) {
    
var cell = document.createElement("div");
    cell.setAttribute(
"className","title");
    cell.innerHTML 
= text;
    
    
return cell;
}

function createCellWithContent(content) {
    
var cell=document.createElement("div")
    cell.innerHTML 
= content; 
    
    
return cell;
}


</script>
</head>
<style>
    .title
{background-color:#CCCCCC; border:#0000FF solid 1px;}

</style>
<body>
  
<h2>Blog文章列表</h2>
  http://www.cnblogs.com/nuke/rss
<br />
  http://blog.sina.com.cn/myblog/index_rss.php?uid=1190363061
<br /> 
   
<input type="text" id="targeturl" name="textfield" />
    
<input type="button" value="搜索" onclick="ShowRSS();"/>    
    
<div id="resultsTitle">
    
</div>
</body>
</html>