PHP程序员站--PHP编程开发平台
 当前位置:主页 >> 网页制作 >> Ajax >> 

一段用ajax读取rss的代码

一段用ajax读取rss的代码

来源:互联网  作者:未知  发布时间:2008-01-11
以下为代码: 以下为引用的内容: scripttype = text/

以下为代码:

以下为引用的内容:
<script type="text/javascript"
var 
xmlHttp

function 
createXMLHttpRequest
() 

     if(
window.ActiveXObject

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

     { 
         
xmlHttp=new XMLHttpRequest
(); 
     } 




function 
startRequset
() 


     
createXMLHttpRequest
(); 
     
xmlHttp.onreadystatechange=handleStateChange

     
xmlHttp.open("get",document.getElementById("url").value,true
); 
     
xmlHttp.send(null
); 




function 
handleStateChange
() 

     if(
xmlHttp.readyState==4

     { 
         if(
xmlHttp.status==200

             { 
                 
document.getElementById("Item4").innerHTML=LoadRSS(xmlHttp.responseXML,5
); 
             } 
     } 


function 
LoadRSS(RssUrl,ShowNewNum



                 var 
xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0"
); 
                                 var 
title,link

                                 var 
strTemp

                                 
                                 
xmlDoc.async false

                                 
xmlDoc.load(RssUrl
); 

                                 if (
xmlDoc.parseError.errorCode != 0
) { 
                                 var 
myErr xmlDoc.parseError

                                 return 
"读取数据发生错误!"
;} 
                                 else 
                                 { 
                                     
title=xmlDoc.selectSingleNode("//rss/channel/title"
);     
                                     
link=xmlDoc.selectSingleNode("//rss/channel/link"
); 

                                     
strTemp="<table><tr><td><a href='"+link.text+"' target='_blank'>"+title.text+
"</a></td></tr>" 
                                     
var channel

                                     var 
strtitle,strlink

                                     
channel=xmlDoc.getElementsByTagName("item"
); 
                                     for(var 
i=0;i<ShowNewNum;i
++) 
                                     { 
                                     
//var Iitem=channel; 
                                     
strtitle=channel[i].getElementsByTagName('title'
);     


                                     
strlinkchannel[i].getElementsByTagName('link'
);     
                                     
strTemp+="<tr><td><a href='"+strlink.item(0).text+"' target='_blank'>"+strtitle.item(0).text+"</a></td></tr>"

                                     
//strTemp+="<tr><td>"+strtitle.item(0).text+"</td></tr>" 
                                     

                                     
strTemp+="</table>"

                                     return 
strTemp

                                 } 


</script> 

     
HTML代码: 

以下为引用的内容:
<form action="#"> 
     <label> 
<input name="url" type="text" id="url" /> 
<input name="button" type="button" onclick="startRequset();" value="获得RSS"/> 
     </label> 
<div     id="Item4" >读取数据中...</div> 
</form>

Tags: ajax   rss   代码   代码   xmlhttp    
最新文章
推荐阅读
月点击排行榜
PHP程序员站 Copyright © 2007-2010,PHPERZ.COM All Rights Reserved 粤ICP备07503606号