Hi there, I'm a newbie of iGoogle and Gadget.
When I was trying my first gadget, an error of "gadgets.io" occured.
The code was coped directly from the "Working with Remote Content"
chapter of gadgets.* Developer's Guide, and I added it to my iGoogle
page through the GGE in the "Developer Tools" chapter. The whole code
is listed below:
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Fetch XML" scrolling="true"/>
<UserPref
name="mycalories"
display_name="Calorie limit"
default_value="800"/>
<UserPref
name="mychoice"
display_name="Show Descriptions"
datatype="bool"
default_value="false"/>
<Content type="html">
<![CDATA[
<div id="content_div"></div>
<script type="text/javascript">
// get prefs
var prefs = new gadgets.Prefs();
// Calorie limit set by user
var calorieLimit = prefs.getString("mycalories");
// Indicates whether to show descriptions in the breakfast
menu
var description = prefs.getBool("mychoice");
function makeDOMRequest() {
var params = {};
params[gadgets.io.RequestParameters.CONTENT_TYPE] =
gadgets.io.ContentType.DOM;
var url = "http://gadget-doc-examples.googlecode.com/svn/trunk/
opensocial-gadgets/breakfast-data.xml";
gadgets.io.makeRequest(url, response, params);
};
function response(obj) {
// Start building HTML string that will be displayed in
<div>.
// Set the style for the <div>.
var html = "<div style='padding: 5px;background-color:
#ccf;font-family:Arial, Helvetica;"
+ "text-align:left;font-size:90%'>";
// Set style for title.
html +="<div style='text-align:center; font-size: 120%; color:
yellow; "
+ "font-weight: 700;'>";
// obj.data contains a Document DOM element corresponding to
the
// page that was requested
var domdata = obj.data;
// Display menu title. Use getElementsByTagName() to retrieve
the <menu> element.
// Since there is only one menu element in the file,
// you can get to it by accessing the item at index "0".
// You can then use getAttribute to get the text associated
with the
// menu "title" attribute.
var title = domdata.getElementsByTagName("menu").item
(0).getAttribute("title");
// Alternatively, you could retrieve the title by getting the
menu element node
// and calling the "attributes" function on it. This returns
an array
// of the element node's attributes. In this case, there is
only one
// attribute (title), so you could display the value for the
attribute at
// index 0. For example:
//
// var title = domdata.getElementsByTagName("menu").item
(0).attributes.item(0).nodeValue;
html += title + "</div><br>";
// Get a list of the <food> element nodes in the file
var itemList = domdata.getElementsByTagName("food");
// Loop through all <food> nodes
for (var i = 0; i < itemList.length ; i++) {
// For each <food> node, get child nodes.
var nodeList = itemList.item(i).childNodes;
// Loop through child nodes. Extract data from the text nodes
that are
// the children of the associated name, price, and calories
element nodes.
for (var j = 0; j < nodeList.length ; j++) {
var node = nodeList.item(j);
if (node.nodeName == "name")
{
var name = node.firstChild.nodeValue;
}
if (node.nodeName == "price")
{
var price = node.firstChild.nodeValue;
}
if (node.nodeName == "calories")
{
var calories = node.firstChild.nodeValue;
}
// If the user chose to display descriptions and
// the child node is "#cdata-section", grab the
// contents of the description CDATA for display.
if (node.nodeName == "description" && description==true)
{
if (node.firstChild.nodeName == "#cdata-section")
var data = node.firstChild.nodeValue;
}
}
// Append extracted data to the HTML string.
html += "<i><b>";
html += name;
html += "</b></i><br>";
html += " ";
html += price;
html += " - ";
// If "calories" is greater than the user-specified calorie
limit,
// display it in red.
if(calories > calorieLimit) {
html += "<font color=#ff0000>";
html += calories + " calories";
html += " </font>";
}
else
html += calories + " calories";
html += "<br>";
// If user has chosen to display descriptions
if (description==true)
{
html += "<i>" + data + "</i><br>";
}
}
// Close up div
html += "</div>";
document.getElementById('content_div').innerHTML = html;
};
gadgets.util.registerOnLoadHandler(makeDOMRequest);
</script>
]]>
</Content>
</Module>
But when I opened iGoogle page in FireFox 3.0.4, the FireBug gave me
the error message of "gadgets.Prefs is not a constructor errFunc()ifr?
url=...lYDDgg.js (行 8)[Break on this error] var prefs = new
gadgets.Prefs();".
Then I tried removing all the gadgets,* invoked except the gadgets.io,
but it still did not work with the error message "gadgets.io is
undefined".
Did I miss some key steps? Or do I have to refer to the gadgets.* lib
explicitly in my code?
Looking forward for your appreciated help.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"iGoogle Developer Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Gadgets-API?hl=en
-~----------~----~----~----~------~----~------~--~---