Ajax|rss
Some time ago participated in the first AJAX contest, the results of a few, the first round has not been, depressed in ~ ~ ~
One of the problems is to use AJAX technology to read RSS seeds, the feeling of learning Ajax technology is very enlightening, and now put my implementation code published, for everyone to learn and exchange!
Topic:
Use XMLHttpRequest to read the ajaxcn.org RSS feed (http://ajaxcn.org/exec/rss) and list the retrieved blog entries with an HTML listing label (UL/OL/LI).
Code implementation:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<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://ajaxcn.org/exec/rss", 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>
<body>
<form action= "#" >
<input type= "button" value= "Search"/>
</form>
<div id= "Resultstitle" ></div>
</body>
You can run it in a Web container, I run it in Tomcat 5.5, and I can read the RSS feeds correctly!