欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Javascript Ajax異步讀取RSS文檔具體實(shí)現(xiàn)

 更新時(shí)間:2013年12月12日 15:03:11   作者:  
這篇文章主要介紹了Javascript Ajax異步讀取RSS文檔具體實(shí)現(xiàn),有需要的朋友可以參考一下

RSS 是一種基于 XML的文件標(biāo)準(zhǔn),通過(guò)符合 RSS 規(guī)范的 XML文件可以簡(jiǎn)單實(shí)現(xiàn)網(wǎng)站之間的內(nèi)容共享。Ajax 是Asynchronous JavaScript and XML的縮寫。通過(guò) Ajax 技術(shù)可以經(jīng)由超文本傳輸協(xié)議(Http) 向一個(gè)服務(wù)器發(fā)出請(qǐng)求并且在等待該響應(yīng)時(shí)繼續(xù)處理另外的數(shù)據(jù)。通過(guò) Ajax 技術(shù)可以很容易實(shí)現(xiàn)讀取遠(yuǎn)程 XML文件,因此,可以使用 Ajax技術(shù)實(shí)現(xiàn)遠(yuǎn)程訪問(wèn)依據(jù) RSS 標(biāo)準(zhǔn)生成的摘要信息,甚至我們可以自己寫一個(gè) RSS 閱讀器。

        Ajax 并不是一門新的語(yǔ)言或技術(shù), 它實(shí)際上是幾項(xiàng)技術(shù)按一定的方式組合在一起。共同在協(xié)作中發(fā)揮各自的作用, 它包括:使用XHTML 和CSS 標(biāo)準(zhǔn)化呈現(xiàn); 使用DOM 實(shí)現(xiàn)動(dòng)態(tài)顯示和交互; 使用XML 和XSLT 進(jìn)行數(shù)據(jù)交換與處理; 使用XMLHttpRequest進(jìn)行異步數(shù)據(jù)讀取; 最后用 JavaScript 綁定和處理所有數(shù)據(jù)。好了,對(duì)于理論就不在多說(shuō)了,下面我們直接看代碼吧。

        創(chuàng)建XMLHttpRequest對(duì)象并將請(qǐng)求發(fā)送到服務(wù)器:

復(fù)制代碼 代碼如下:

function createXHR(url){
     if(window.XMLHttpRequest){
         xmlHttp = new XMLHttpRequest();
     }else{ 
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttp.open("post",url,"false");
    xmlHttp.onreadystatechange = getResponse;     xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(null);
 }

通過(guò)DOM操作對(duì)Rss文檔進(jìn)行遍歷,得到需要的值:

復(fù)制代碼 代碼如下:

function readDoc(doc){
    root = doc.getElementsByTagName("channel")[0];
    docTitle = root.getElementsByTagName("title")[0];
    docLink = root.getElementsByTagName("link")[0];
    docDescription = root.getElementsByTagName("description")[0];
    items = root.getElementsByTagName("item");
    for(var i=0;i<items.length;i++){
        itemTitle = items[i].getElementsByTagName("title")[0];
        itemLink = items[i].getElementsByTagName("link")[0];
        itemDescription = items[i].getElementsByTagName("description")[0];
        //itemPubDate = items[i].getElementsByTagName("pubDate")[0];
        document.getElementById("rssTitle").innerHTML = docTitle.firstChild.nodeValue;
        temp = "</h1><h2><a href=""+itemLink.firstChild.nodeValue+"" target="_blank">"+itemTitle.firstChild.nodeValue+"</a></h2>"+"<p>"+itemDescription.firstChild.nodeValue+"</p><hr/>";
        document.getElementById("readRss").style.display = "none";
        document.getElementById("printRss").getElementsByTagName("span")[0].style.display = "none";
        document.getElementById("printRss").innerHTML = document.getElementById("printRss").innerHTML + temp;
    }
}

調(diào)用createXHR(url)函數(shù),傳入?yún)?shù),向服務(wù)器發(fā)送求:

復(fù)制代碼 代碼如下:

createXHR("http://www.apple.com.cn/hotnews/rss/hotnews.rss");

得到響應(yīng):

復(fù)制代碼 代碼如下:

function getResponse(){
   if(xmlHttp.readyState == 4){     
        if(xmlHttp.status == 200){ 
            rssDoc = xmlHttp.responseXML;
            readDoc(rssDoc);//調(diào)用readDoc()函數(shù)
        }else{
            document.getElementById("rssTitle").innerHTML = "讀取異常!";
            //alert(xmlHttp.status);
        }
    }
}

相關(guān)文章

最新評(píng)論