Getting Started introduces you to Google Gadgets and the scratchpad, which lets you edit gadgets and preview your changes without even leaving the developer guide. The next step is creating your own gadgets. This document tells you how.
Here are the basic steps you follow to create and deploy a gadget:
See Google Gadgets Editor: Get Started Now for additional publishing options available through GGE.
You can use File > Upload to upload gadget resources into GGE. Once they are hosted on GGE, you can use them in your gadgets. For example, to upload an image you want to include in a gadget:
http://hosting.gmodules.com/ig/gadgets/file/114770342902047654452/myimage.jpg.
You can use this URL to reference the image file in your gadget
spec. If you are not authoring, hosting, and publishing your gadget through GGE, you must find another way to host it on an external server. Two alternatives are Google Page Creator and Google Code hosting. You can add any gadget to iGoogle directly by typing the gadget's URL into the Add by URL text box in the iGoogle content directory.
The developer gadget acts a "command center" for all of the gadgets on your iGoogle page. In addition to listing all the gadgets that you're running, it lets you add, view, and manage gadgets. The developer gadget gives you features that you will need if you're doing gadget development. For example, it lets you add gadgets that are "broken," which is useful when you are actively changing a gadget.
To add the developer gadget, click here:
In the developer gadget you can click on individual gadget links to view their XML specifications. This is a good way to see how other gadgets are implemented.
You don't need to worry about advanced developer gadget features for now. They are discussed in more detail here. But one feature you may want to take advantage of right away is the Cached checkbox. By default, gadget specifications are cached. You should uncheck Cached for gadgets while you are working on them. This lets you see your edits instead of the cached gadget.
Once you understand how to edit and publish gadgets, you're ready to include more advanced features in your gadget specifications. The XML gadget specification consists of 3 major parts:
<Content> section
is where the real work of your gadget happens. It is where
you specify the type of gadget, your programming logic, and
often the HTML elements that determine the appearance of your
gadget.<UserPrefs> section
defines controls that allow users to specify settings for the
gadget. For example, a personalized greeting gadget might provide
a text field for users to specify their names.<ModulePrefs> section
in the XML file specifies characteristics of the gadget, such
as title, author, preferred sizing, and so on.Note: Within the XML attributes in a gadget spec, you need to "escape" (that is, properly encode) certain characters so that they will be interpreted correctly. For more information, see Escaping Special Characters.
When writing a gadget, you should start with the <Content> section.
The <Content> section represents
the "brains" of a gadget. The <Content> section
defines the type of content, and either holds the content itself
or has a link to external content. The <Content> section
is where the gadget attributes and user preferences are combined
with programming logic and formatting information to become a running
gadget.
The easiest way to create your gadget is to simply place HTML (and
optionally, JavaScript or Flash) into the <Content> section.
Experienced web developers can read Choosing
a Content Type for other options relating to access control,
remote hosting, using alternative scripting languages, and other
topics. Here's a simple sample gadget. This gadget displays
a clickable photograph that opens a photo album in a new HTML page:
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Go to Photo Album" height="250" scaling="false" />
<Content type="html">
<![CDATA[
<div style="text-align:center"><a
id="Riggs" title="My Photo Album" target="_blank"
href="http://picasaweb.google.com/doc.examples/ShelfBoy">
<img border="0" alt="Photo" src="http://doc.examples.googlepages.com/Riggsie-OP.jpg"
title="Click Here."></a>
</div>
]]>
</Content>
</Module>
This is the running gadget:
Some gadgets need to give users a way of supplying user-specific
information. For example, a game gadget might allow users to enter
a preferred level of difficulty. The user preferences (<UserPref>)
section in the XML file describes the user input fields that are
turned into user interface controls when the gadget runs. User
preferences are stored persistently.
For example, this gadget displays a personal greeting based on the time of day. It lets users specify the following:
This is an example of the running gadget:
This is what the gadget looks like when the user clicks edit to modify the user preferences:
The user preferences that get turned into user interface controls in the running gadget are defined in the XML specification as follows:
<?xml version="1.0" encoding="UTF-8" ?> <Module> <ModulePrefs title="Preferences for __UP_myname__" height="250" />
<UserPref name="myname" display_name="Name" required="true" />
<UserPref name="myphoto" display_name="Photo" default_value="http://doc.examples.googlepages.com/Rowan-headshot.jpg"/>
<UserPref name="mychoice" display_name="Show Photo?" datatype="bool" default_value="true"/>
<UserPref name="mycolor" display_name="Color" default_value="Yellow" datatype="enum" > <EnumValue value="Red" /> <EnumValue value="Aqua" /> <EnumValue value="Lime" /> <EnumValue value="Yellow" /> <EnumValue value="Pink" /> <EnumValue value="Orange" /> <EnumValue value="White" /> </UserPref>
Note the following:
title="Preferences for __UP_myname__". When you run the gadget, the value supplied for the user preference myname is dynamically substituted for __UP_myname__.bool data type. This is displayed in the user interface as a checkbox.enum data
type. The list of EnumValues specifies
the choices that appear in a drop-down menu in the user preferences
edit box. Here is the JavaScript that displays the greeting for the gadget:
<Content type="html">
<![CDATA[
<div id="content_div" style="font-size:12pt; padding:5px;">
<script type="text/javascript">
// Get userprefs
var prefs = new _IG_Prefs();
// Based on user input, display personal greeting
function displayGreeting (){
// Create Date object
var today = new Date();
// Get current time.
var time = today.getTime();
var salutation;
// Based on the time of day, display an appropriate greeting
var hour = today.getHours();
if (hour < 12)
salutation = "Morning";
else if (hour > 17)
salutation = "Evening";
else salutation = "Afternoon";
// Build HTML string to display message
var html = "";
var element = document.getElementById('content_div');
element.style.height=250;
// Set the background color according to the mycolor userpref
element.style.backgroundColor=prefs.getString("mycolor");
// Display a greeting based on the myname userpref
html += "<br><h2>Good " + salutation + ", " + prefs.getString("myname") + "!!!</h2><br>";
// If the "Show Photo?" checkbox is checked, display photo.
if (prefs.getBool("mychoice")==true)
{
html += '<img src="' + prefs.getString("myphoto") + '">';
}
_gel("content_div").innerHTML = html;
}
_IG_RegisterOnloadHandler(displayGreeting);
</script>
]]>
</Content>
For a list of all the <UserPref> attributes, see the Reference.
User preferences are accessed from your gadget using the user preferences JavaScript API, for example:
<script type="text/javascript">
var prefs = new _IG_Prefs();
var someStringPref = prefs.getString("StringPrefName");
var someIntPref = prefs.getInt("IntPrefName");
var someBoolPref = prefs.getBool("BoolPrefName");
</script>
For a list of all of the JavaScript functions, see the Reference.
Note: If you store sensitive private user data in a gadget's user preferences, we suggest that you use the locked-domain feature.
You can use a substitution variable of the format __UP_userpref__ in
the <ModulePrefs> or <UserPref> sections,
where userpref matches
the name attribute of a user preference. When the gadget
runs, the string value of the corresponding user preference
is substituted for the variable, unescaped. For example, in
this excerpt, the value the user supplies at run time for the projects user
preference is substituted for __UP_projects__ in
the title_url string:
<Module>
<ModulePrefs title="Build Monitor"
title_url="http://www.example.com/build/status.php?__UP_projects__"/>
<UserPref name="projects" display_name="project(s)"/>
<Content ... />
</Module>
You can see another example of this in the user preferences sample.
Here are the general guidelines for using user preference substitution variables:
The <ModulePrefs> section in the XML file specifies characteristics of the gadget, such as title, author, preferred sizing, and so on. For example:
<Module>
<ModulePrefs title="Today's Network Traffic" title_url="http://www/~rowan/gadgets/stats/"
height="200" author="Jane Smith" author_email="xxx@google.com"/>
<Content ...>
... content ...
</Content>
</Module>
The users of your gadget cannot change these attributes.
For a full list of the <ModulePrefs> attributes, see the Reference.
Remember that there is no such thing as a private gadget. Once you publish your gadget on a public website, people can find it and view it. Be sure not to include personal information, such as your telephone number or personal email address.
What if you don't want your gadget to be public? Google encourages gadget authors to share their specifications. However, if you want to minimize your gadget's public visibility before you are ready to release it, here are some tips:
When you're ready to write more complex gadgets, go to Development Fundamentals, or back to the documentation home page for an overview of sections and topics.