抑制safari中的图像加载错误

时间:2009-07-06 02:44:31

标签: javascript safari image

如果对http://localhost:someportnumber/icon的调用返回零大小200响应,我试图显示图像。

在大多数浏览器中,以下(缩减)有效: jQuery。$('#myImage')。load(function(){   jQuery的$(本).show()。 });

如果图像没有加载,img元素永远不会显示,它的一切都很好。

但是,safari决定将这些失败的图像加载报告为页面错误: http://img.skitch.com/20090706-kna755s8fbb1pbd3mf9di9hdkg.jpg

尽我所能,我还没有找到解决这些错误的好方法。任何指向正确方向的人都会非常感激。

2 个答案:

答案 0 :(得分:1)

旧版本的Safari已损坏且image.onload无法正常运行。

所以,为了处理它,我已经使用了一些用脚本加载图像的东西,然后测试它们是否在那里。

简化,类似于:

// 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);
  }
}

经过一段时间后,我们会检查哪些图像未加载,并在我们的gui中替换它们。

我希望safari-hack至少有用:)

答案 1 :(得分:1)

您是否尝试插入if函数来检查图像是否存在,然后仅在加载时才加载?

您还可以在

中添加一些内容
<img src="blah" onerror="function to remove image if it doesn't load">