Javascript 检查图像是否存在

Javascript 检查图像是否存在,javascript,jquery,ajax,Javascript,Jquery,Ajax,我使用以下代码在浏览器中刷新图像。我只想修改代码,以便首先检查图像是否存在,然后显示图像。如果图像不存在,我只会将图像刷新到图片的以前版本。有人能告诉我如何使用javascript或jquery实现这一点吗 <html> <head> <script language="JavaScript"> function refreshIt(element) { setTimeout(fu

我使用以下代码在浏览器中刷新图像。我只想修改代码,以便首先检查图像是否存在,然后显示图像。如果图像不存在,我只会将图像刷新到图片的以前版本。有人能告诉我如何使用javascript或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>
大概是这样的:

$('#image_id').error(function() { alert('Image does not exist !!'); }); $('#image_id')。错误(函数(){ 警报('图像不存在!!'); });
我可以将我已经实现的功能与此结合起来吗?这不会按原样工作(演示:),要使用它,您必须使用jQuery设置
src
属性,然后链接
error()
方法: $('#image_id').error(function() { alert('Image does not exist !!'); });