HTML if image is not found

后端 未结 16 2007
温柔的废话
温柔的废话 2020-12-07 13:46

I have an image in a HTML page:

\"Smiley

If the image is not found o

相关标签:
16条回答
  • 2020-12-07 13:59

    Here Check below code snippet which, In this, I miss-spelled the image name. So, just because of that it is showing the alternative image as not found image ( 404.svg ).

    <img id="currentPhoto" src="https://www.ccrharrow.org/Images/content/953/741209.pg" onerror="this.src='https://www.unesale.com/ProductImages/Large/notfound.png'" alt="">

    0 讨论(0)
  • 2020-12-07 13:59

    This one worked for me. using the srcset. I have just learnt about it so i dont know if browsers support it but it has worked for me. Try it and later give me your feed back.

     <img src="smiley.gif" srcset="alternatve.gif" width="32" height="32" />
    
    0 讨论(0)
  • 2020-12-07 14:00

    The usual way to handle this scenario is by setting the alt tag to something meaningful.

    If you want a default image instead, then I suggest using a server-side technology to serve up your images, called using a similar format to:

    <img src="ImageHandler.aspx?Img=Blue.jpg" alt="I am a picture" />
    

    In the ImageHandler.aspx code, catch any file-not-found errors and serve up your default.jpg instead.

    0 讨论(0)
  • 2020-12-07 14:00

    try PHP

    if (file_exists('url/img/' . $Image)) {
        $show_img_URL = "Image.jpg";
    } else {
        $show_img_URL = "Image_not_found.jpg";
    }
    
    0 讨论(0)
  • 2020-12-07 14:01

    Try using border=0 in the img tag to make the ugly square go away.

    <img src="someimage.png" border="0" alt="some alternate text" />

    0 讨论(0)
  • 2020-12-07 14:06

    If you want an alternative image instead of a text, you can as well use php:

    $file="smiley.gif";
    $alt_file="alt.gif";
    if(file_exist($file)){
         echo "<img src='".$file."' border="0" />";
    }else if($alt_file){
         // the alternative file too might not exist not exist
         echo "<img src='".$alt_file."' border="0" />";
    }else{
      echo "smily face";
    }
    
    0 讨论(0)
提交回复
热议问题