javascript:检查图像src是否存在 [英] javascript: check if image src exists

查看:73
本文介绍了javascript:检查图像src是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多预加载脚本,但似乎没有任何一个可以解决我的问题。


假设我有一个图像列表:

" image1.jpg"

" image2.jpg"

" image3.jpg"


以及两者都托管的所有照片:
http://mydomain.com/images /
http://mydomain2.com/images/


我想尝试加载图片:
http://mydomain.com/images/

如果失败,请从镜像站点获取它们。


它可以接受的是,如果在第一个位置找不到一个图像,绕过它并从第二个位置获取所有图像。



最初我有这样的东西:

I have seen a lot of pre-loading scripts and none of them seem to help with my problem.

Let''s say I have a list of images:
"image1.jpg"
"image2.jpg"
"image3.jpg"

And all the pictures hosted at both:
http://mydomain.com/images/
http://mydomain2.com/images/

I would like to attempt to load pictures from:
http://mydomain.com/images/
and if they fail, get them from the mirror site.

It is acceptable that if one image cannot be found on the first location, to bypass it and get ALL the images from the second location.


Originally I had something like this:

展开 < span class =codeDivider> | 选择 | Wrap | Line数字

推荐答案

尝试使用完成属性。您可以使用setInterval / setTimeout检查图像在一段时间后是否已加载。
Try using the complete property. You could use setInterval/setTimeout to make a check that the image has loaded after a period of time.



尝试使用完成属性。您可以使用setInterval / setTimeout检查图像在一段时间后是否已加载。
Try using the complete property. You could use setInterval/setTimeout to make a check that the image has loaded after a period of time.



我看到了完整的属性,看到IE / FF / opera有它,但它不是标准的一部分所以我没有再看看它。

你会说这样做会安全吗?


所以我尝试使用它:

I saw the complete property, and saw that IE/FF/opera have it, but that it wasn''t part of the standards so I didn''t look into it any more.
Would you say it would be safe to do so?

So I tried using it:

展开 | 选择 | Wrap | 行号


否,该属性称为完成你在Image对象上使用它,而不是在它的源头上使用它。
No, the property is called "complete" and you use it on the Image object, not on its source.


这篇关于javascript:检查图像src是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆