您好,登錄后才能下訂單哦!
這篇文章主要介紹AJAX如何實現(xiàn)RSS無刷新聚合器,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
rss.js
代碼如下:
<!--
//控制ID
function getId(objId){
return document.getElementById(objId)
}
//鏈接對應(yīng)數(shù)組,請注明轉(zhuǎn)載自http://www.cnrui.cn/blog
function urlArrary(urlName){
switch(urlName){
case "clear":
return "http://www.cnrui.cn/blog/feed.asp"
case "a-fei":
return "http://www.a-fei.net/blog/rss.xml"
case "ryanxia":
return "http://www.ryanxia.com/blog/feedrss2.asp"
case "awflasher":
return "http://www.awflasher.com/blog/feed.asp"
case "seles":
return "http://www.cnrui.cn/seles/feedrss2.asp"
case "wawa":
return "http://a-fei.net/wawa/blog/rss2.php"
case "dreammx":
return "http://www.dreammx.com/xhyl/feed.asp"
default:
return false
}
}
//側(cè)邊欄控制,請注明自http://www.cnrui.cn/blog轉(zhuǎn)載
function closebar(targetid,objId){
if(document.getElementById){
target = getId(targetid);
objsty = getId(objId);
if(target.style.display == "none"){
target.style.display = "";
main.style.width = "800px";
objsty.innerHTML = "關(guān)閉側(cè)邊欄"
}else{
target.style.display = "none";
main.style.width = "100%";
objsty.innerHTML = "開啟側(cè)邊欄"
}
}
}
//加載變灰,注明自http://www.cnrui.cn/blog轉(zhuǎn)載
function listen(a){
getId("prolist").className=a;
}
function btnSty(objId,objStyle)
{
getId(objId).style.display = objStyle
}
//提示窗口控制
function ifodis(){
btnSty("loadifo","none");
}
function settime(a){
setTimeout(a,1000);
}
function loadtime(){
getId("loadifo").innerHTML += "."
}
//數(shù)據(jù)入口
function ent(urlName){
window.location.hash = urlName
var frmUrl = saveState(urlName);//寫入信息到ifream
var gamestart = loadrss(urlName);//AJAX數(shù)據(jù)過程
}
//XML數(shù)據(jù)綁定
var xmldoc = false;
function loadrss(urlName){
var myurl = urlArrary(urlName)
if(myurl){
listen("mainbg2");
//生成隨機數(shù),同時限制刷新次數(shù)
var Num=Math.floor(Math.random()*100);
//定義往址參數(shù)
url = myurl+"?id="+Num;
//定義為異步傳輸模式
xmldoc = false;
//Mozill,Safari等瀏覽器時需要創(chuàng)建的XMLHttp類
if(window.XMLHttpRequest){
xmldoc = new XMLHttpRequest();
if(xmldoc.overrideMimeType){
xmldoc.overrideMimeType('text/xml');
}
}
//IE瀏覽器時創(chuàng)建的XMLHttp類
else if(window.ActiveXObject){
try{
xmldoc = new ActiveXObject("Msxml3.XMLHTTP");
}
catch(e){
try{
xmldoc = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xmldoc = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
}
//不能創(chuàng)建XMLHTTP類時返回
if(! xmldoc){
return false;
}
//調(diào)用CheckState函數(shù)
xmldoc.onreadystatechange = CheckState;
xmldoc.open('GET',url,true);
xmldoc.send(null);
}else{
getId("listnum").innerHTML = "您輸入的地址錯誤,請重試!"
}
}
//狀態(tài)檢測
function CheckState(){
var response = xmldoc.responseXML.documentElement;
btnSty("loadifo","block")
//收到完整的服務(wù)器響應(yīng)
if(xmldoc.readyState == 1){
getId("loadifo").innerHTML = "連接服務(wù)器"
}
else if(xmldoc.readyState == 2){
getId("loadifo").innerHTML = "開始加載數(shù)據(jù)"
}
else if(xmldoc.readyState == 3){
getId("loadifo").innerHTML = "正在加載數(shù)據(jù)"
settime(loadtime)
}
else if(xmldoc.readyState == 4){
//HTTP服務(wù)器響應(yīng)值成功
if(xmldoc.status == 200){
//將服務(wù)器返回的字符串寫到頁面中ID為showdiv的區(qū)域
getId("loadifo").innerHTML = "加載完成"
settime(ifodis)
num(response)
}
else{
getId("loadifo").innerHTML = "錯誤"+xmldoc.status+"點這里刷新";
}
}
}
//檢查數(shù)據(jù)合法性
function trydata(nodeName,nodeName2){
try{
var nodeV = nodeName.firstChild.nodeValue;
}catch(e){
try{
var nodeV = nodeName2.firstChild.nodeValue;
}catch(e){
var nodeV = "沒有可用信息"
}
}
return nodeV;
}
//記錄集總數(shù)
function num(b){
try{
var titleNode = b.selectNodes("//rss/channel/title");
var a = b.getElementsByTagName("item").length
titleV = trydata(titleNode(0));
getId("listnum").innerHTML = "共有"+a+"篇文章 來源于:"+titleV;
setlist(a,b);
}catch(e){}
}
if (top.location != self.location)top.location=self.location;
//節(jié)目列表顯示
//定義接收指向各個元素的節(jié)點信息的變量
var titleNode,linkNode,categoryNameNode,authorNode,pubDateNode,descriptionNode
//定義接收元素值的變量
var titleV,linkV,categoryNameV,authorV,pubDateV,descriptionV
//分類聚合
function setlist(a,b)
{
var prograNode = b.getElementsByTagName("item");
var adminlinkNode = b.selectNodes("//rss/channel/link");
var titleNode = b.selectNodes("//rss/channel/item/title");
var linkNode = b.selectNodes("//rss/channel/item/link");
var categoryNode = b.selectNodes("//rss/channel/item/category");
var authorNode = b.selectNodes("//rss/channel/item/author");
var pubDateNode = b.selectNodes("//rss/channel/item/pubDate");
var descriptionNode = b.selectNodes("//rss/channel/item/description");
var category2sNode = b.selectNodes("//rss/channel/item/dc:subject");
var author2sNode = b.selectNodes("//rss/channel/item/dc:creator");
var pubDate2sNode = b.selectNodes("//rss/channel/item/dc:date");
var listchar=""
//使用for循環(huán),產(chǎn)生列表信息
for (i=0; i<a; i++)
{
titleV = trydata(titleNode(i))
linkV = trydata(linkNode(i))
categoryV = trydata(categoryNode(i),category2sNode(i))
authorV = trydata(authorNode(i),author2sNode(i))
pubDateV = trydata(pubDateNode(i),pubDate2sNode(i))
descriptionV = trydata(descriptionNode(i))
adminlinkV = trydata(adminlinkNode(0))
//將得到的元素值裝載到產(chǎn)生表格的字符串中
listchar += "<h6><a href='"+linkV+"' target='_blank'>"+titleV+"</a></h6><span>發(fā)表時間:"+pubDateV+"</span><div class='article'>"+descriptionV+"</div><div class='bottom'>文章分類:"+categoryV+" <a href='"+adminlinkV+"' target='_blank'>作者:"+authorV+"</a></div>";
}
//將生成的表格輸出到頁面的DIV元素
getId("prolist").innerHTML = listchar;
listen("mainbg")
}
//瀏覽器回退部分
//重寫IFREAM鏈接,瀏覽器回退重點
function saveState(message) {
var hisFrm = getId("historyFrame")
hisFrm.src = "blank.html?"+message
}
//檢測IFREAM
window.dhtmlHistory = {
iframeLoaded:function(newLocation){
var hash = new String(newLocation.search)
if (hash.length >= 2 && hash.charAt(0) == "?" && hash.substring(1) !=delChar()){
hash = hash.substring(1)
window.location.hash = hash;
var hisFrmdata = loadrss(hash)
}
}
}
//瀏覽器鏈接指向部分
//去除符號,請注明http://www.cnrui.cn/blog轉(zhuǎn)載
function delChar(){
var a = window.location.hash
return a.substring(1)
}
window.onload = function(){
if(window.location.hash.length >= 2){
ent(delChar())
}
}
// -->
以上是“AJAX如何實現(xiàn)RSS無刷新聚合器”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。