Javascript 抑制Safari中的图像加载错误
如果调用Javascript 抑制Safari中的图像加载错误,javascript,safari,image,Javascript,Safari,Image,如果调用http://localhost:someportnumber/icon返回大小为零的200响应 在大多数浏览器中,以下(缩减)功能有效: jQuery.$('#myImage').load(函数(){ jQuery.$(this.show(); }); 如果图像未加载,则永远不会显示img元素,并且一切正常 但是,Safari决定将这些失败的图像加载报告为页面错误: 尽管我已经尝试了很多次,但我还没有找到一个好方法来避免这些错误。任何指向正确方向的指针都将不胜感激。旧版本的Saf
http://localhost:someportnumber/icon
返回大小为零的200响应
在大多数浏览器中,以下(缩减)功能有效:
jQuery.$('#myImage').load(函数(){
jQuery.$(this.show();
});
如果图像未加载,则永远不会显示img
元素,并且一切正常
但是,Safari决定将这些失败的图像加载报告为页面错误:
尽管我已经尝试了很多次,但我还没有找到一个好方法来避免这些错误。任何指向正确方向的指针都将不胜感激。旧版本的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);
}
}
//加载图像
var img=新图像();
img.src=”http://whatever/image.jpg";
imageList.push(img);
: : :
//检查
pollsafarimages();
//我们有一个民意调查员来处理狩猎活动,而onerror没有
//将“this”返回到图像对象,因此我们不知道是哪个
//它指的是一个
函数pollSafariImages()
{
var loadedCount=0;
var指数;
对于(index=0;index您是否尝试插入if函数来检查图像是否存在,然后仅在图像存在时加载
你也可以在盒子里放些东西
<img src="blah" onerror="function to remove image if it doesn't load">
onerror=”“是导致safari报告错误的原因。我可以处理该属性中的错误,但afaict在js中无法阻止safari报告错误。(即,它实际上不是js错误),类似于使用if函数进行测试。失败的测试会导致safari报告错误
<img src="blah" onerror="function to remove image if it doesn't load">