This is a single archived entry from Stefan Tilkov’s blog. For more up-to-date content, check out my author page at INNOQ, which has more information about me and also contains a list of published talks, podcasts, and articles. Or you can check out the full archive.

Download Feeds with JavaScript

Stefan Tilkov,

The Google AJAX Feed API:

With the AJAX Feed API, you can download any public Atom or RSS feed using only JavaScript, so you can easily mash up feeds with your content and other APIs like the Google Maps API.

Example usage:

function initialize() {
var feed = new google.feeds.Feed("http://www.digg.com/rss/index.xml");
feed.load(function(result) {
if (!result.error) {
var container = document.getElementById("feed");
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var div = document.createElement("div");
div.appendChild(document.createTextNode(entry.title));
container.appendChild(div);
}
}
});
}

Very neat. Puzzling at first sight, though, is the header:

<script type="text/javascript" src="http://www.google.com/jsapi?key=YOUR_KEY_HERE"></script>

Why would one load a script from there instead of just downloading it? The answer is security — Google acts a a proxy, so the JS code never accesses anything other than the domain it was loaded from.