检查图像存在 [英] Check if image exist

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

问题描述

我用下面的code刷新图像在浏览器中。我只是想修改code,以先检查图像存在,然后显示图像。如果图像不存在我将只刷新图像到图像的previous版本。有人能指出我如何做到这一点使用JavaScript或jQuery的?

 < HTML>
    < HEAD>
        < SCRIPT LANGUAGE =JavaScript的>
            功能refreshIt(元){
                的setTimeout(函数(){
                    element.src = element.src.split('?')[0] +'?' +新的日期()的getTime()。
                    refreshIt(元);
                },500); //刷新每500毫秒
            }
        < / SCRIPT>
    < /头>
    <身体GT;
        < IMG SRC =swt.pngNAME =myCam的onload =refreshIt(本)>
    < /身体GT;
< / HTML>
 

编辑:我需要的已经实现的功能加上文件检查相结合

功能:

 如果图像存在
  刷新图像
其他
  显示缓存图像
 

解决方案

事情是这样的:

$('#image_id)。错误(函数(){
  警报('图片不存在!);
});

I use the following code to refresh an image in the browser. I just want to modify the code in order to first check if the image exists and then display the image. If the image does not exist I will only refresh the image to the previous version of the picture. Can someone point me how to accomplish this using javascript or jquery?

<html>
    <head>
        <script language="JavaScript">
            function refreshIt(element) {
                setTimeout(function() {
                    element.src = element.src.split('?')[0] + '?' + new Date().getTime();
                    refreshIt(element);
                }, 500); // refresh every 500ms
            }
        </script>
    </head>
    <body>
        <img src="swt.png" name="myCam" onload="refreshIt(this)">
    </body>
</html>

Edited: I need a combination of the already implemented functionality plus the file checking.

Functionality:

if image exist 
  refresh image
else 
  show cached image

解决方案

Something like this:

$('#image_id').error(function() {
  alert('Image does not exist !!');
});

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

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