Changes between Version 3 and Version 4 of UsingFeedsInCode


Ignore:
Timestamp:
May 11, 2006, 5:25:21 PM (15 years ago)
Author:
nick
Comment:

add javascript example

Legend:

Unmodified
Added
Removed
Modified
  • UsingFeedsInCode

    v3 v4  
    1212
    1313== !JavaScript ==
     14
     15Note that the JavaScript will either need to come from the same server as the feed, or be running with the browser granting it extra security rights, otherwise you'll get a security exception when you try to request something off another server.
     16
     17An example for pulling out titles and modified dates from the recent changes RSS feed, and chucking them into a paragraph with the id "data" follows.
     18
     19{{{
     20// This will fetch the Recent Changes RSS from an OpenGuide on the
     21//  same server as the script.
     22// It will then output the node title and modified date, into a
     23//  paragraph on the page with id "data"
     24// You will need to ensure that there is:
     25//      <p id="data" />
     26// Already on the page before calling this
     27var feed_url = "/og-wiki/wiki.cgi?action=rc;format=rss";
     28
     29function go() {
     30    // Get a XMLHttpRequest Object
     31    var xmlhttp = false;
     32    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
     33        xmlhttp = new XMLHttpRequest();
     34    } else {
     35        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     36    }
     37
     38    // Use it
     39    if(xmlhttp) {
     40        xmlhttp.open("GET", feed_url, false);
     41        xmlhttp.send(null);
     42
     43        var status = xmlhttp.status;
     44        var p = document.getElementById("data");
     45
     46        if(status == 200 || status == 304 || status == 0 || status == null) {
     47            // Bingo, we have the feed
     48            var feed = xmlhttp.responseXML;
     49
     50            // Go find the individual item entries
     51            var items = feed.getElementsByTagName("item");
     52
     53            // Loop over the items, getting the titles and dates out
     54            for(var i=0; i<items.length; i++) {
     55                var item = items[i];
     56                var ic = item.childNodes;
     57
     58                var title;
     59                var date;
     60                for(var j=0; j<ic.length; j++) {
     61                    var tag = ic[j];
     62                    if(tag.nodeType == Node.ELEMENT_NODE) {
     63                        if(tag.tagName == "title") {
     64                            title = tag;
     65                        }
     66                        if(tag.tagName == "dc:date") {
     67                            date = tag;
     68                        }
     69                    }
     70                }
     71
     72                // Add a div with the title and date
     73                var sub_div = document.createElement("div");
     74                sub_div.appendChild(document.createTextNode(
     75                    title.firstChild.nodeValue + " - modified " + date.firstChild.nodeValue
     76                ));
     77                p.appendChild(sub_div);
     78            }
     79        } else {
     80            var text = "Error code " + status;
     81            p.appendChild(document.createTextNode(text));
     82        }
     83    }
     84}
     85go();
     86}}}