热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> AJAX编程>>正文内容

用Ajax读取RSS种子的简单例子

发布时间:2009年02月09日点击数: 未知
<!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 createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }  
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}
    
function readRSS() {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET", "http://rss.sina.com.cn/news/china/focus15.xml", 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 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;

        addListRow(title);
    }

}


function addListRow(title) {
    var row = document.createElement("ul");
    var cell = createCellWithText(title);
    row.appendChild(cell);
    
    document.getElementById("resultsTitle").appendChild(row);
}

function createCellWithText(text) {
    var cell = document.createElement("li");
    var textNode = document.createTextNode(text);
    cell.appendChild(textNode);
    
    return cell;
}
</script>
</head>

<body onload="readRSS();">
  <h2>Blog文章列表</h2>
    <div id="resultsTitle"></div>
</body>
</html>

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作