Main
Latest
- Google Social Graph API and PHP 5.2
- Google AJAX Feed API Muti Example
- Gnip API Changes
- Google Maps and Geolocation
- oEmbed, flickr and starstar
- Petition Against Public Holidays
- Good Morning
- Django Apache Configuration
- Raising funds for Autism Western Cape
- When to release the code?
Archives
- June 2004
- July 2004
- August 2004
- September 2004
- October 2004
- November 2004
- December 2004
- January 2005
- February 2005
- March 2005
- April 2005
- May 2005
- June 2005
- July 2005
- August 2005
- September 2005
- October 2005
- November 2005
- December 2005
- January 2006
- February 2006
- March 2006
- April 2006
- May 2006
- June 2006
- July 2006
- August 2006
- September 2006
- November 2006
- December 2006
- January 2007
- February 2007
- March 2007
- April 2007
- May 2007
- June 2007
- July 2007
- August 2007
- September 2007
- October 2007
- November 2007
- December 2007
- January 2008
- February 2008
- March 2008
- April 2008
- May 2008
- June 2008
- July 2008
- August 2008
- September 2008
- October 2008
- November 2008
- December 2008
- January 2009
Custom Muti Widgets
Here is some code, again making use of the Google AJAX Feed API, but this time you roll your own DOM so you have the world's flexibility of how you want the data to be displayed on your site.
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Google AJAX Feed API</title>
<!-- Load the Google JavaScript library -->
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<!-- Add our own scripts -->
<script type="text/javascript">
// Decide which RSS feed you want to load
var feedUri = "http://muti.co.za/by?name=charlvn&output=rss";
// Load Google's feed engine
google.load("feeds", "1");
/**
* Do the actual magic in this function. The content
* gets dynamically loaded and pushed into the DOM.
*/
function loadMuti() {
// Disable the button before we start loading
var button = document.getElementById("mutibutton");
button.disabled = true;
// Load the actual data via Google
var feed = new google.feeds.Feed(feedUri);
// Decide what happens after the feed has been loaded
feed.load(function(result) {
if (result.error) {
// If the feed could not load, enable the button again
button.disabled = false;
} else {
// Create and insert the necessary elements into the DOM
var ul = document.getElementById("muti");
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
// Create an anchor for the link
var a = document.createElement("a");
a.href = entry.link;
a.appendChild(document.createTextNode(entry.title));
// Create an item to insert into the unordered list
var li = document.createElement("li");
li.appendChild(a);
ul.appendChild(li);
}
}
});
}
/**
* Ensure the button is enabled before refreshing,
* otherwise some browsers might keep it disabled
* even after the page has been reloaded.
*/
function resetMuti() {
document.getElementById("mutibutton").disabled = false;
}
</script>
<style type="text/css">
/**
* This will apply to all elements in the DOM,
* whether it is sourced from the original HTML
* or dynamically generated via scripting.
*/
#muti li a {
color: white;
background: green;
}
</style>
</head>
<body onunload="resetMuti()">
<!-- This is the unordered list where the links will be populated -->
<ul id="muti"></ul>
<!-- Here is the button triggering the ajax action -->
<button id="mutibutton" onclick="loadMuti()">Load My Muti Submissions</button>
</body>
</html>
I tried to document it well so that you can see what's going on. Please comment if you have any questions though!
It's a real pity that we can't call google.load("feeds", "1") only after the user clicks the button. That would mean that if they do not click the button we can avoid loading the whole feed library. However this causes strange behaviour in many browsers, probably some document.write inside of the library or something of the sort, not sure.
This has been tested in Firefox 3.0.1 on Linux and seems to work fine. Not sure about other browsers. This is not something that should be used, it's just a demo, you build your own thing and use this as an example for learning. :)
We are still trying to support HTML 5 as far as possible, naturally.
Oh yes and as a last note. This should work with any public Atom or RSS feed, not just with muti.
I have a live demo of the code up if anybody wants it.
Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.


0 Comments
Post a Comment