This article mainly introduces the JavaScript Ajax asynchronous read RSS document implementation, the need for friends can refer to the
RSS is an xml-based file standard that can be easily shared between Web sites through XML files that conform to the RSS specification. Ajax is the abbreviation for asynchronous JavaScript and XML. Ajax technology allows a request to be made to a server via Hypertext Transfer Protocol (HTTP) and continues processing additional data while waiting for the response. It is easy to read remote XML files through AJAX technology, so you can use AJAX technology to implement remote access to digest information generated by RSS standards, and even we can write an RSS reader ourselves. Ajax is not a new language or technology, it is actually a combination of several technologies in a certain way. Play their roles together in collaboration, which includes: standardizing rendering using XHTML and CSS; Use DOM to implement dynamic display and interaction; Data exchange and processing using XML and XSLT; Using XMLHttpRequest for asynchronous data reading; Finally, you bind and process all the data with JavaScript. Well, for the theory is not to say more, the following we look directly at the code. Create XMLHttpRequest objects and send requests to the server: The code is as follows: function createxhr (URL) { &NBSP ; if (window. XMLHttpRequest) { xmlhttp = new XMLHttpRequest (); }else{ &NBSP ; xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP"); } XMLHTTP.O Pen ("post", URL, "false"); Xmlhttp.onreadystatechange = GetResponse; Xmlhttp.setrequestheader ("Content-type", "Application/x-www-form-urlencoded "); xmlhttp.send (null); &NBSP} Iterate through the RSS document through the DOM operation to get the desired value: code is as follows: function Readdoc (DOC) { root = DOC.G Etelementsbytagname ("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]; & nbsp //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; } } call CREATEXHR (URL) function, pass in the parameter, send the request to the server: code as follows: CREATEXHR ("http://www.apple.com.cn/ Hotnews/rss/hotnews.rss "); Get response: Code as follows: Function GetResponse () { if (xmlhttp.readystate = = 4) { &NBS P if (Xmlhttp.status = 200) { Rssdoc = xmlhttp.responsexml; & nbsp Readdoc (Rssdoc)//Call Readdoc () function }else{ D Ocument.getelementbyid ("Rsstitle"). InnerHTML = "Read Exception!" "; //alert (xmlhttp.status); } &nbs P } }