Home  >  Article  >  Web Front-end  >  Perfectly compatible with multiple browsers, js judgment image path code summary_javascript skills

Perfectly compatible with multiple browsers, js judgment image path code summary_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:03:182164browse

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

Copy code The code is as follows:
090ef25f054448c759e8ee90c6fb3a8a

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn