Hi there,

I have some simple code that goes off and loads a bunch of tiled images into a web page. What I am trying to do is replace one of the tiles with a blank image in the event the image does not exist on the server. I am using the Image() object onError event to do this, except, my test url function does not seem to be returning the hardcoded URL to my blank image. Any idea why not? Here is my code:

function get_my_url (bounds) {
//ignore all of this rubbish...
var res = this.map.getResolution();
var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
var y = Math.round ((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
var z = this.map.getZoom();
var path = z + "/" + y + "_" + x + "." + this.type; 
var url = this.url;

if (url instanceof Array) {
	url = this.selectUrl(path, url);
	}

fullurl = url + path;
//here is the call to my image test function which should return my blank png URL
testImage(fullurl);
return fullurl;
}
function testImage(fullurl) {

    var tester=new Image();
    tester.src=fullurl;

    tester.onerror = function (evt) {
     fullurl="http://www.laudontech.com//temp//vladopenlayers//Z17/65536_65539.png"
return fullurl;
 
  }
   
   tester.onload = function (evt) {
  
   alert(tester.src + " is loaded.");
  
  }
}

Mapper,

Your onerror handler does nothing more than return (into a big void) the url of your default image. A return from a function attached as an event handler is completely meaningless because there's nothing (other than inaccessible native code) to receive the returned value. No error is thrown to warn you.

You need to get the onerror handler to do something with the url rather than returning it.

Try this:

tester.onerror = function(evt) {
		fullurl = "http://www.laudontech.com//temp//vladopenlayers//Z17/65536_65539.png"
		if(this.src !== fullurl) { this.src = fullurl; }
	}

Note the safety test to ensure the browser doesn't get into an infinite iteration, which is a possibility if the default image also fails (though maybe browsers proof themselves against this, I don't know - anyway no harm taking the precaution).

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.