Perfectly compatible with multi browser js judge image path code summary

  • 2020-05-30 19:24:38
  • OfStack

The first way


// Check if the picture exists 
function CheckImgExists(imgurl) {
var ImgObj = new Image(); // Determine if the image exists 
ImgObj.src = imgurl;
// No picture, return -1
if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {
alert(imgurl + ' There are ');
} else {
alert(imgurl + ' There is no ');
}
}
CheckImgExists('/editor/simple/FileIconDir/rar.gif');
CheckImgExists('/editor/simple/FileIconDir/rarsdfs.gif');

Another way to do it

<img src=" The original image " alt=" The picture " onerror="this.src=' The image '">

The third way


<script language="javascript" type="text/javascript">
// Call this function on load failure 
function defaultImg(T)
{
T.src = "images/closelabel.gif";// Default image address 
}

// This function is called after successful loading 
function LoadOK()
{
alert("LoadOK");
}
</script>
</head>

<body>
<img src="123.jpg" onerror="defaultImg(this);" onload="LoadOK()"/>
  <!--123.jpg  It is the wrong image address -->

That's all for this article, I hope you enjoy it.


Related articles: