欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用ajax实现读博客rss示例代码

程序员文章站 2022-06-29 12:34:05
复制代码 代码如下:
复制代码 代码如下:

<!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>this is test</title>

<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(url) {
createxmlhttprequest();
xmlhttp.onreadystatechange = handlestatechange;
xmlhttp.open("get", url, true);
xmlhttp.send(null);


}

function handlestatechange() {
if(xmlhttp.readystate == 4) {
if(xmlhttp.status == 200) {
resultset();
}
}
}

function resultset() {
var results = xmlhttp.responsexml;
var title = null;
var item = null;
var link=null;
//得到channel
var ccc=results.getelementsbytagname("channel");
var headtitle=ccc[0].getelementsbytagname("title")[0].firstchild.nodevalue;
var headlink=ccc[0].getelementsbytagname("link")[0].firstchild.nodevalue;
var cell = document.createelement("div");
cell.innerhtml="<h1><a href="+headlink+" target=_blank>"+headtitle+"</a></h1>
";
document.getelementbyid("result").appendchild(cell);
//得到items
var items = results.getelementsbytagname("item");
for(var i = 0; i < items.length; i++) {
item = items[i];
link=item.getelementsbytagname("link")[0].firstchild.nodevalue;
title = item.getelementsbytagname("title")[0].firstchild.nodevalue;
var cell = document.createelement("div");
cell.innerhtml="<a href="+link+" target=_blank>"+title+"</a>
";
document.getelementbyid("result").appendchild(cell);
}

}
function readrss1()
{
var url=document.getelementbyid("txturl").value;
if(url=="")
{
alert("请输入rss地址");
}
else
{
readrss(url);
}
}

</script>
</head>

<body">
<h1>ajax读rss示例</h1>
<form >

<a href="javascript:readrss('http://www.blogjava.net/rss.aspx')">blogjava原创区 </a>
<a href="javascript:readrss('http://beginner.blogjava.net/rss.aspx')">blogjava新手区 </a>
<a href="javascript:readrss('http://life.blogjava.net/rss.aspx')">blogjava非技术区 </a>
<a href="javascript:readrss('http://general.blogjava.net/rss.aspx')">综合区 </a>


输入一个rss地址:<input type="text" value="http://www.blogjava.net/wujun/rss.aspx" size=50 id="txturl">
<input type="button" value="查 看" onclick="readrss1()">

</form>
<div id="result"></div>
</body>
</html>