Home >Web Front-end >JS Tutorial >Perfectly compatible with multiple browsers, js judgment image path code summary_javascript skills
The first way
//检查图片是否存在 function CheckImgExists(imgurl) { var ImgObj = new Image(); //判断图片是否存在 ImgObj.src = imgurl; //没有图片,则返回-1 if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) { alert(imgurl + '存在'); } else { alert(imgurl + '不存在'); } } CheckImgExists('/editor/simple/FileIconDir/rar.gif'); CheckImgExists('/editor/simple/FileIconDir/rarsdfs.gif');
Another implementation method
The third way
<script language="javascript" type="text/javascript"> //载入失败调用此函数 function defaultImg(T) { T.src = "images/closelabel.gif";//默认图片地址 } //载入成功后会调用此函数 function LoadOK() { alert("LoadOK"); } </script> </head> <body> <img src="123.jpg" onerror="defaultImg(this);" onload="LoadOK()"/> <!--123.jpg 是错误图片地址-->
The above is the entire content of this article, I hope you all like it.