views:

303

answers:

3

I am trying to show an image if a call to http://localhost:someportnumber/icon returns a zero size 200 response.

in most browsers, the following (cut down) works: jQuery.$('#myImage').load(function() { jQuery.$(this).show(); });

if the image doesnt load, the img element is never shown, and its all good.

however, safari decides to report these failed image loads as page errors: http://img.skitch.com/20090706-kna755s8fbb1pbd3mf9di9hdkg.jpg

as much as i have tried, i havent found a good way of getting around these errors. any pointers in the right direction would be much appreciated.

A: 
  1. This may help you!
Srikanth
thanks, however this doesnt solve my problem. the problem is not in the i am handling the load event in the javascript. safari is reporting the error outside of the js as "can't locate host". i was hopeful with that link, though after trying it, it appears the error occurs even if the <img> element isnt inserted into the dom.
gcrain
A: 

The older version of Safari is broken and the image.onload doesn't working properly.

So, to deal with it, I have used something where the images are loaded by script, then tested if they are there.

Simplified, something along the lines of:

// load the images
var img = new Image();
img.src = "http://whatever/image.jpg";
imageList.push(img);

: : :

// check
pollSafariImages();


// we have a poller to handle Safari as the onerror does not
// return 'this' to the image objects, so we don't know which
// one it was referring to
function pollSafariImages()
{
  var loadedCount = 0;

  var index;
  for (index=0; index<imageList.length; index++)
  {
    if (imageList[index].bLoaded == true)
    {
      loadedCount++;
    }
    else if (imageList[index].complete == true)
    {
      imageList[index].bLoaded = true;
    }
  }
  if (loadedCount < imageList.length)
  {
    setTimeout('pollImages()', 250);
  }
}

After a duration we check which images aren't loaded and replace them in our gui.

I hope the safari-hack is at least useful :)

cdm9002
A: 

Have you tried inserting an if function to check that the image exists, and then only loading if it does?

You could also put something in the

<img src="blah" onerror="function to remove image if it doesn't load">
Sneakyness
the onerror="" is what is causing safari to report the error. i can handle the error in that attribute, but afaict there is no way in js to stop safari from reporting it. (ie. its not actually a js error)similarly with testing with an if function. the failed test causes safari to report an error
gcrain