How to Write an Ajax RSS Script

Asynchronous JavaScript and XML, AJAX, is a technique using several web technologies to develop interactive client-side web applications. JavaScript is used to read an XML file, and display formatted content on the Web page using Dynamic HTML (DHTML) and Document Object Model (DOM). Really Simple Syndication (RSS) feed files are XML formatted files containing information and links for news stories, blog posts, and even podcast information. Fetching and displaying an RSS feed is a great way to get started with AJAX.

Things You'll Need

  • A text editor, such as Notepad++
  • RSS file on your Web site
Show More

Instructions

    • 1

      Create a new JavaScript file. Save it with the file name "rss.js".

    • 2

      Add the following function. The function intializes and returns an XMLHttpRequest object. XMLHttpRequest is the core of AJAX programming. Older versions of Internet Explorer initialize an XMLHttpRequest using an ActiveX instead of the window.

      function initXmlHttpReq() {
      var x ;
      if (window.XMLHttpRequest) {
      x = new XMLHttpRequest();
      }
      else if (window.ActiveXObject) {
      x = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else {
      alert("Your browser does not support this feature.");
      return ;
      }

      return x ;
      }

    • 3

      Add the following function to handle the action of parsing and displaying the RSS feed when the file is fetched. readyState indicates the status of the file fetch, and is equal to 4 when the file was fetched successfully. The document will have a DIV tag with the id "rss." The script will write the RSS into this DIV.

      function stateChange() {
      if (xmlreq.readyState==4) {
      var data = xmlreq.responseXML;
      var elem = document.getElementById("rss");
      parseRss(data, elem);
      }
      }

    • 4

      Add the following two functions for parsing and writing the RSS data stored in the data object. Each RSS headline is contained in the "item" element, which contains the title, link, and other information in child elements. parseRss builds an unordered list, populates it with the information from the RSS feed, and writes it to a DIV with the id "rss" in the web page using your script. getVal is used to pull data from the child nodes, and keeps the parseRss function a little more neat and readable.

      function parseRss(data, elem) {
      elem.innerHTML = "<h2>Feed<h2>";
      var items = data.getElementsByTagName("item");
      var list = document.createElement("ul");
      for (var i = 0 ; i < items.length ; i++) {
      var listItem = document.createElement("li");
      var linkItem = document.createElement("a");
      linkItem.setAttribute("href", getVal(items[i],"link"));
      linkItem.appendChild(document.createTextNode(getVal(items[i],"title")));
      listItem.appendChild(linkItem);
      list.appendChild(listItem);
      }
      elem.appendChild(list);
      }

      function getVal(obj, tag) {
      return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
      }

    • 5

      Add the following function that opens the RSS feed, triggering the parsing and writing of the contents to the web page.

      function makeRequest () {
      xmlreq.open("GET", url, true);
      xmlreq.send(null);
      }

    • 6

      Add the following lines at the end of the script. These lines will execute when the page loads. Change the URL "rss.xml" to the URL for your RSS feed.

      var xmlreq = initXmlHttpReq();
      var url = "rss.xml" ;
      xmlreq.onreadystatechange=stateChange;

    • 7

      Use the script by including the script in your web page's head area:
      <script type="text/javascript" src="rss.js"/>

      and adding the call to "makeRequest()" to the BODY tag's onload attribute:

      <body onload="makeRequest();">

Tips & Warnings

  • Customize the formatting by modifying the parseRSS function or creating a style in your CSS file for "div.rss", "div.rss ul", and "div.rss ul li". Update the RSS automatically by creating a timer event in your JavaScript to call the makeRequest function every few minutes. Create a ticker that displays one headline at a time by modifying the for loop in parseRSS to wait a few seconds before replacing the contents of the DIV with the next headline in the list.

  • AJAX can only be used to fetch XML files from servers on your own network domain. To use external RSS feeds, use a server-side scheduler to fetch copies to your Web server, or a server-side web application to fetch and deliver feeds to your AJAX script. Also see the "MDC: HTTP Access Control" link in the References for additional information about implementing cross-site HTTP requests.

Related Searches:

References

Resources

Comments

You May Also Like

  • How to Create RSS Alerts for a Web Page

    RSS feeds started to gain widespread use on the Internet in 2005 and 2006. An RSS feed is a standardized XML-based formatted...

  • How to Create an AJAX Web Site

    Asynchronous JavaScript and XML (AJAX) is a technique of updating web page content without reloading the entire page. AJAX allows developers to...

  • How Do I Add an RSS Reader to My Web Page?

    RSS is a syndication system for publishing feeds of content from blogs and news websites. Having an RSS feed reader on your...

  • How to Syndicate My Content Using RSS

    RSS has become popular by the blog explosion, and more of your users are downloading content to their readers everyday. There are...

  • How to Write a Radio Script

    Learning how to write a radio script is critical for proper execution of a radio performance. The script must include various cues...

  • How to Write Your Own Function in Excel 2007

    Excel is capable of complicated calculations. In some cases, it's easier to understand than a calculator, and far more versatile--even allowing you...

  • How to Write a CSS Script

    This article will explain how to set up a CSS page when you are writing a program for a website. A properly...

  • AJAX Tutorial: JavaScript

    AJAX (Asynchronous JavaScript and XML) is a name for several techniques used to create interactive Web applications. Asynchronous loading means that if...

  • How to Use RSS Feeds in Outlook 2007

    Outlook 2007 has added the ability to access and manage syndicated feeds or websites and blogs that provide "Really Simple Syndication," or...

Related Ads

Featured