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
return fullurl;
function testImage(fullurl) {

    var tester=new Image();

    tester.onerror = function (evt) {
return fullurl;
   tester.onload = function (evt) {
   alert(tester.src + " is loaded.");
7 Years
Discussion Span
Last Post by Airshow


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).


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.