看看我编的JS脚本,可非常准确地判断出图像的大小、尺寸。当然也就可以判断图像是否存在了。<form method="POST" action="" name=testupload>
<input type="file" name="t1" size="20">
</form>
<p><img border="0" src="" id=preview></p>
<input type="button" name="up" value="doUpload" onclick="getImgSize(testupload.t1.value)">
<script>          
waitTime=0;    
oldSize=preview.fileSize;    
    
tid=null;    
    
function getImgSize(Imgpath){          
preview.src=Imgpath;          
waitTime=2;  
wait();    
alert("大小是:"+oldSize+"\n\n尺寸是:宽="+preview.width+",高="+preview.height);//这里可改成其它应用 
}     
    
//等待图像全部显示完    
function wait(){    
if (waitTime==0 || preview.fileSize!=oldSize) {    
if ( tid!=null) clearTimeout(tid);     
oldSize=preview.fileSize;    
wiatTime=0;   
return;    
}    
else {    
tid=setTimeout("wait()",1000);    
}    
    
}         
</script>

解决方案 »

  1.   

    try
    <img id="myimg" src="http://www.gif" onerror="alert('no such image')">
      

  2.   

    check its readyState property, for example:<img id="myimg" src="http://www.gif" onerror="alert('no such image')">
    <script language="javascript">
    function window.onload()
    {
      alert(myimg.readyState);
      alert(myimg.readyState == "uninitialized");
    }
    </script>
      

  3.   

    to:karma
    错误
    我的意思你可能还没有理解
    我不过是想判断这个元素在不再,如果这个元素根本就不存在的话,
    就会报错.出现myimg未定义
    应该你说的这个不是我要的那种
      

  4.   

    function x_center()
    {
      it=eval(bgimg); //你的图片的ID。例如<img name="bgimg" src="1.gif">
      if(it.complete)
      {
        alert("完成打开文件.");
      }
      else
        setTimeout('x_center()',50);
    }
    x_center();
      

  5.   

    <img name='bgimg' src=""></img>
    这一句一定要在x_center();之前。
    并且x_center();之前还要赋值:
    bgimg.src="http://www.topcool.net/cgi-bin/Count.cgi?df=agaist.dat&md=6&ft=3&dd=C&frgb=ff0000";
    //your picture;
      

  6.   

    我的意思可能您还没有懂.就是
    <img name='bgimg' src=""></img>
    可能就没有这个元素
    因为我是用的循环,如果没有的话.那就没有这个img,那就不会有这句话了
    是不是
      

  7.   

    首先图片的加载要一定时间,我的程序就是判断图片是否成功加载了。
    如果你连这个网页文件中是否存在<img name='bgimg' src=""></img>这么一个元素都不知道的话,可以通过
    document.images.length
    document.images[i]
    来判断存在性。
      

  8.   

    Reve说的对,图片的加载要一定时间,不通过判断图片的属性,怎么知道SRC中的内容是真是假。也就无法准确性了。
      

  9.   

    you should check the attribute for images in onload event or later, since after onload event, all image files should have been loaded <script language="javascript">
    function window.onload()
    {
      for (var i=0; i < document.images; i++)
      {
        if (document.images[i].readyState == "uninitialized")
        {
         alert(document.images[i].href + " is not loaded, it is probably a bad link");
        }
      }}
    </script>
      

  10.   

    sorry, change
    for (var i=0; i < document.images; i++)
    ===>
    for (var i=0; i < document.images.length; i++)
      

  11.   

    我实在是笨的没有办法了
    我还是把程序给你们贴出来把
    你们看看呢
    <%for i=1 to 2 
      %>
    <img name="img" id="outimg<%=i%>" src="xxxx">
    <%next%>
    <script language='javascript'>
    function imgfunction(){
    if (outimg1.src!="")
    {
    if (outimg1.width>210) outimg1.width=210;
    if (outimg1.height>160) outimg1.height=160;
    }
    if (document.showIMG.length!="")
    {
    if (outimg2.width>210) outimg2.width=210;
    if (outimg2.height>160) outimg2.height=160;
    }
    }
    我试了你们这个length方法不行,因为还是无法判断呢
      

  12.   

    真是分数不多,道理深。
    看了你的代码,src="xxxx"不是有图片文件吗?
    xxxx是什么? 是变量(写法不对)? 就认为是变量,也只是在服务器端对xxxx赋值(图片文件名),好!就用FileSystemObject去判断这个图片文件是否存在吧。另外showIMG在那里?哈哈!一定写错了,大概是outimg2吧。
      

  13.   

    <%for i=1 to 2 
      %>
    <img name="img" id="outimg<%=i%>" src="xxxx">
    <%next%>
    <script language='javascript'>
    function imgfunction()
    {
    if (outimg1.readyState != "uninitialized")
    {
          if (outimg1.width>210) outimg1.width=210;
          if (outimg1.height>160) outimg1.height=160;
    }
             else
                outimg1.style.display = "none";
    if (outimg2.readyState != "uninitialized")
    {
          if (outimg2.width>210) outimg2.width=210;
          if (outimg2.height>160) outimg2.height=160;
    }
             else
                outimg2.style.display = "none";
    }
    window.onload = imgfunction;
    </script>
      

  14.   

    不好意思
    怪我没有解释清楚,
    for i=1 to 2
    是一个从数据中取出的循环,有时候只能有一个,有时候是两个
    也就是说有一个就循环一个
    有两个就循环两个
    那karma这样的写法还是没有定义对象阿
    我也是这么写的
      

  15.   

    <%for i=1 to 2 
      %>
    <img name="img" id="outimg<%=i%>" src="xxxx">
    <%next%>
    <script language='javascript'>
    function imgfunction()
    {
         for (var i=1; i <= <%=i%>; i++)
         {
             var myimg = eval("outimg"+i);
             if (myimg)
             {
       if (myimg.readyState != "uninitialized")
       {
          if (myimg.width>210) myimg.width=210;
          if (myimg.height>160) myimg.height=160;
       }
                else
                   myimg.style.display = "none";
             }
          }
    }
    window.onload = imgfunction;
    </script>