利用AJAX读取RSS
现在我们利用这个框架来读取RSS,代码如下:
ReadRSS.html
代码片段
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>ReadRSS</title>
<script language="javascript">
var XML_Http_Request = false;
function createXMLHttpRequest(){
XML_Http_Request = false;
if(window.XMLHttpRequest){ //for Mozilla
XML_Http_Request = new XMLHttpRequest();
if(XML_Http_Request.overrideMimeType){
XML_Http_Request.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject){ //for IE
try{
XML_Http_Request = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
XML_Http_Request = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
}
function send_Request(url){
/*The function send_request has some other parameters.
function send_request(url,Parameter1,Parameter2,...,Parametern){
*/
createXMLHttpRequest();
if(!XML_Http_Request){
window.alert("Cannot create XMLHttpRequest instance!");
return false;
}
XML_Http_Request.onreadystatechange = processRequest;
/*The function processRequest has some parameters.
XML_Http_Request.onreadystatechange = function(){
processRequest(Parameter1,Parameter2,...,Parametern);
};
*/
XML_Http_Request.open("GET",url,true); //true---异步;false---同步
XML_Http_Request.send(null);
}
function processRequest(){
/*The function processRequest has some parameters.
function processRequest(Parameter1,Parameter2,...,Parametern){
*/
if(XML_Http_Request.readyState == 4) {
if(XML_Http_Request.status == 200) {
/***************************************************************************/
//statements
var results = XML_Http_Request.responseXML;
var title = null;
var item = null;
var link = null;
var description = null;
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><br>";
document.getElementById("result").appendChild(cell);
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 = "<li><a href="+link+" target=_blank>"+title+"</a></li><br>";
document.getElementById("result").appendChild(cell);
}
/*******************************************************************************/
}
}
}
</script>
</head>
<body onLoad="javascript:send_Request(’http://esoft.bokee.com/rss2.xml’);">
<div id="result">
</div>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>ReadRSS</title>
<script language="javascript">
var XML_Http_Request = false;
function createXMLHttpRequest(){
XML_Http_Request = false;
if(window.XMLHttpRequest){ //for Mozilla
XML_Http_Request = new XMLHttpRequest();
if(XML_Http_Request.overrideMimeType){
XML_Http_Request.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject){ //for IE
try{
XML_Http_Request = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
XML_Http_Request = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
}
function send_Request(url){
/*The function send_request has some other parameters.
function send_request(url,Parameter1,Parameter2,...,Parametern){
*/
createXMLHttpRequest();
if(!XML_Http_Request){
window.alert("Cannot create XMLHttpRequest instance!");
return false;
}
XML_Http_Request.onreadystatechange = processRequest;
/*The function processRequest has some parameters.
XML_Http_Request.onreadystatechange = function(){
processRequest(Parameter1,Parameter2,...,Parametern);
};
*/
XML_Http_Request.open("GET",url,true); //true---异步;false---同步
XML_Http_Request.send(null);
}
function processRequest(){
/*The function processRequest has some parameters.
function processRequest(Parameter1,Parameter2,...,Parametern){
*/
if(XML_Http_Request.readyState == 4) {
if(XML_Http_Request.status == 200) {
/***************************************************************************/
//statements
var results = XML_Http_Request.responseXML;
var title = null;
var item = null;
var link = null;
var description = null;
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><br>";
document.getElementById("result").appendChild(cell);
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 = "<li><a href="+link+" target=_blank>"+title+"</a></li><br>";
document.getElementById("result").appendChild(cell);
}
/*******************************************************************************/
}
}
}
</script>
</head>
<body onLoad="javascript:send_Request(’http://esoft.bokee.com/rss2.xml’);">
<div id="result">
</div>
</body>
</html>
相关文档(Relevant Entries)
Posted on May 21, 2007 8:10 PM
| Permalink | | | TrackBacks (0)
TrackBack URL for this entry:
http://www.wujianrong.com/mt-tb.cgi/5279
http://www.wujianrong.com/mt-tb.cgi/5279