4 messages in com.googlegroups.google-desktop-developerRe: Display image from URL
FromSent OnAttachments
Robert22 Jun 2008 18:56 
HemS22 Jun 2008 20:23 
James [GD Team]23 Jun 2008 00:18 
Robert23 Jun 2008 10:20 
Subject:Re: Display image from URL
From:Robert (Prof@gmail.com)
Date:06/23/2008 10:20:04 AM
List:com.googlegroups.google-desktop-developer

The url seems to be correct (using a debug trace to see what is entered). I can type the url and filename into a browser address bar, and the proper image is displayed.

Here is the full main.js, in case that would help

// Portions of this code are Copyright (c) 2007 Google Inc.

function SimpleXmlParser(xmlDoc) { this.xmlDoc = xmlDoc; this.parseError = xmlDoc.parseError; if (this.parseError.errorCode != 0) { debug.error("SimpleXmlParser ERROR: " + this.parseError.reason); } }

function LoadImage(ImageName) { var http = new XMLHttpRequest(); var url = "http://www.watkinsonline.com/images/products/ thumbnails/"+ImageName; http.onreadystatechange = onData; http.open("GET", url, "true"); http.send(); function onData() {

if (http.readyState == 4) { if (http.status == 200) { var source = http.responseStream; imageControl.src = source;

} else { // status != 200 imageControl.src = "placeholder.png"; } } } }

SimpleXmlParser.prototype.getItems = function(key) { var xmlDoc = this.xmlDoc; var items = []; if (this.parseError.errorCode != 0) { debug.error("SimpleXmlParser ERROR: " + this.parseError.reason); } else { var objNodeList = xmlDoc.getElementsByTagName(key); for (var i = 0; i < objNodeList.length; ++i) { var xmlItem = objNodeList.item(i); var item = {}; var added = false; for (var j = 0; j < xmlItem.childNodes.length; ++j) { var child = xmlItem.childNodes.item(j); if (child.childNodes.length > 0) { var name = child.nodeName; var value = child.childNodes[0].nodeValue; item[name] = value; added = true; } } if (added) { items.push(item); } else { debug.trace("item " + i + " did not have any child elements. Omitted.") } } }

var aa = Math.floor(Math.random()*i); var item_title = items[aa]['title']; Product.innerText = item_title; var item_link = items[aa]['link']; Product.href = item_link; var item_image = items[aa]['description'];

//for testing - shows the URL of the image, even if the image does not load link1.innerText=item_image; link1.href=item_image; debug.trace("Image filename " + item_image); LoadImage(item_image);

return items; }

var output = ''; var FEED_URL = 'http://www.e-medtools.com/watkinsfeed.xml';

function onOpen() { var request = new XMLHttpRequest(); request.open("GET", FEED_URL, true); request.send();

function displayItems(theItems) { output += '\n========================================'; output += '\ngot ' + theItems.length + ' results. Parse errors: ' + parser.parseError.reason; for (var i = 0; i < theItems.length; ++i) { for (var key in theItems[i]) { output += '\nItem ' + i + ' has name:value pair ' + key + ': ' + theItems[i][key]; } } }

if (request.status == 200) { var parser = new SimpleXmlParser(request.responseXml);

// get the channel information var items = parser.getItems("channel"); displayItems(items);

// get the items items = parser.getItems("item"); displayItems(items); } else { debug.error("Request failed. HTTP error code: " + request.status); } var xmlDoc = new DOMDocument(); xmlDoc.async = false; var parser = new SimpleXmlParser(xmlDoc); items = parser.getItems("name"); displayItems(items); }

The main.XML is as follows

<view height="100" resizable="false" width="200" onopen="onOpen();"

<img x="1" y="2" src="background.png"/> <a height="29" name="Product" width="176" x="94" y="11" href="http:// www.google.com" >www.google.com </a> <a height="16" name="link1" width="120" x="99" y="44" href="http:// www.google.com" >www.google.com </a> <img height="80" name="imageControl" width="80" x="11" y="12" src="stock_images\image.png" /> <script src="main.js" /> </view>

On Jun 23, 8:18 am, "James [GD Team]" <j.@google.com> wrote:

Hi Robert,

If I type the full URL in, it of course works.  Also, if I type the filename in LoadImage("file.jpg") the image is displayed.

Seems odd. Are you sure the problem is in the LoadImage code? Is the parameter to LoadImage always correct?

Cheers! James

On Jun 22, 6:57 pm, Robert <Prof@gmail.com> wrote:

I am struggling with getting images to display from a URL.  This is what I have so far -

function LoadImage(ImageName) {   var http = new XMLHttpRequest();   var url = "http://www.watkinsonline.com/images/products/ thumbnails/"+ImageName;   http.onreadystatechange = onData;   http.open("GET", url, "true");   http.send();   function onData() {

    if (http.readyState == 4) {       if (http.status == 200) {         var source = http.responseStream;         imageControl.src = source;

      }       else { // status != 200         imageControl.src = "placeholder.png";       }     }   }

}

The imagename variable is read from an XML feed, and I have verified that the image file names are reading correctly.  However, the function above does not display an image.

If I type the full URL in, it of course works.  Also, if I type the filename in LoadImage("file.jpg") the image is displayed.

This is how LoadImage() is called -

//the section on reading the XML feed is omitted....this happens after the feed is parsed

var item_image = items[aa]['description'];

        //for testing - shows the URL of the image, even if the image does not load         link1.innerText=item_image;         link1.href=item_image;

        LoadImage(item_image);

Help would be greatly appreciated.

Thanks