Friday, November 21, 2008

How to create a Google Gadget

Google Gadgets are a great tool, they allow you to have specific information in, what you could call, a small HTML page. You can export your gadgets to iGoogle, Google Desktop, Google Toolbar, to any web page and even you can create a Windows Vista Gadget based on them, isn't that great?

In this post, I am going to walk you through the creation of a simple gadget, but first let's talk about what Google Gadgets are.

Google gadgets are small pieces of code created using XML, HTML, CSS and Javascript that allow you to display data. These gadgets can be embedded in certain applications allowing you to export your information more easily.

They are really easy to create, you just need basic knowledge of HTML and JavaScript and Google provides you with JavaScript APIs to you make your life easier, you can check them in their Legacy Gadgets API Developer's Guide

One of the greatest advantages about a gadget is that they can be used in so many places, but you manage only one source code, so if you need to apply a change to your gadget, you just do it and then all the places were the gadget is used would be updated with no new installation or update required.

Before we start coding, let's talk about a little bit about the gadget components. A Google Gadget is a XML file that contains one big tag called Module that encloses the gadget, then we have two main different components, the module preferences and the gadget content.

The module preferences are tags that you use to set the gadget information like title, author, width, height, etc. In our example, you will see this tag <Require feature="dynamic-height"/>, this tells the gadget parser that the height should be dynamically changed depending on the size of the content. This feature only works with iGoogle though.

The gadget content is where you do all the work, here you need to specify a type, mainly "html" if you want to have your HTML and JavaScript in your gadget or "url" if you want the gadget content to live on a remote web page referenced by a URL in the gadget spec. Inside this tag you can code JavaScript and HTML to layout your data, and CSS if you want to give some style to your content. You need to enclose your code into the tags ![CDATA[ and ]].

For this post, we are going to create a simple gadget that reads the top stories feed and display them using the API called _IG_FetchFeedAsJSON to read the feed as JSON. You will see that this code is really simple and easy to follow.

To work with Google gadgets you need a proper editor, you could use notepad if you want at first, but then you would need a way to publish it. Google already provides a tool called Google Gadget Editor (GGE) which is a gadget itself, so you can embedded it in your iGoogle, which allows you to edit your gadget and preview it in the same editor if want to. You can add it to your iGoogle by clicking here.

Now that you have the GGE in your iGoogle page, copy the following code into your GGE.

<?xml version="1.0" encoding="UTF-8"?>
title=" Top Stories"
author="Your name"
description="Gadget to see the current top stories."
<Require feature="dynamic-height"/>

<Content type="html">
div#main_container {
margin: 5px;
text-align: left;
text-decoration: none;

div#main_container h2 {
background: url( no-repeat left top;
display: block;
height: 33px;
text-indent: -100000;
width: 144px;

<script type="text/javascript">

// Function to dipslay content retrieved from XML feed
function displayContent() {

var url = "";
// Use the _IG_FetchFeedAsJSON() function to retrieve core feed data from
// the specified URL. Then combine the data with HTML markup for display in
// the gadget.
// Let's say we want to display only 10 entries and that we want to display the summary
var entries = 10;
var summary = true;
_IG_FetchFeedAsJSON(url, processFeed, entries, summary);

// Function to process the feed
function processFeed(feed) {
var contentDiv = _gel("main_container");

if (feed == null) {
contentDiv.innerHTML = "Invalid data.";

// Start building HTML string that will be displayed in gadget.
var html = "";

// Access the fields in the feed
html += "<h2>" + feed.Title + "</h2>";
html += "<div>" + feed.Description + "</div><br>";

// Access the data for a given entry
if (feed.Entry) {
for (var i = 0; i < feed.Entry.length; i++) {
html += "<div>" + "<a target='_blank' href='" + feed.Entry[i].Link + "'>" + feed.Entry[i].Title + "</a> ";
// The feed entry Date field contains the timestamp in seconds
// since Jan. 1, 1970. To convert it to the milliseconds needed
// to initialize the JavaScript Date object with the correct date,
// multiply by 1000.
var milliseconds = (feed.Entry[i].Date) * 1000;
var date = new Date(milliseconds);
html += date.toLocaleDateString();
html += " ";
html += date.toLocaleTimeString();
html += "<br><i>" + feed.Entry[i].Summary + "</i>";
html += "</div>";
// Set the html into the main container
contentDiv.innerHTML = html;

// Adjust gadget height according to contents
// Register the function displayContent in the onLoad handler so it is called when the gadget is first loaded


<div id="main_container"></div>


As you can see in the code, the function _IG_FetchFeedAsJSON receives as parameters the feed URL and the name of the callback function in charge of processing and displaying the data.

In the callback function what we do first is to check if the JSON response we received is valid, if it isn't then we just display a message saying that the data is invalid. If the response is valid then we go through the JSON properties to retrieve the data and display it using HTML and CSS.

As you can see this particular API is really easy to use and understand, you just need some basic knowledge of Javascript and that is it!

Now that you have the code in your GGE, you can publish it to your iGoogle so you can see how the dynamic height works depending on the content. Also you can publish it to a web page. Just click on the File menu and select the Publish option. You will get a warning saying that we did not set the Thumbnail preference, but that is not going to affect the code itself, since the Thumbnail is used just to display the gadget Thumbnail in the Google Gadget Directory, so people know how the gadget looks like.

I hope this helps! Post your comments and/or questions!


  1. Hi, I cant use the field Date from blogger xml; I get a error using feed.Entry[i].Date or feed.Entry[i].Published

    You can help me!

    my email is


  2. What error are you getting? Are you sure the value is set in the xml that you are reading?