Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:03:18
Original
2190 people have browsed it

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');

Copy after login

Another implementation method

Copy code The code is as follows:
picture

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 是错误图片地址-->
Copy after login

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template