Javascript Ajax Asynchronous read RSS document specific implementation _javascript skills

Source: Internet
Author: User

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 number of technologies are grouped together 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 the XMLHttpRequest object and send the request to the server:

Copy Code code as follows:

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);
}

Through the DOM operation to the RSS document traversal, get the desired value:

Copy Code code as follows:

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 = "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 the CREATEXHR (URL) function, pass in the parameter, send the request to the server:

Copy Code code as follows:

CREATEXHR ("Http://www.apple.com.cn/hotnews/rss/hotnews.rss");

Get a response:

Copy Code code as follows:

function GetResponse () {
if (xmlhttp.readystate = = 4) {
if (Xmlhttp.status = = 200) {
Rssdoc = Xmlhttp.responsexml;
Readdoc (Rssdoc);//Call Readdoc () function
}else{
document.getElementById ("Rsstitle"). InnerHTML = "Read Exception!" ";
alert (xmlhttp.status);
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.