[英]How to get status code from an image url? Javascript
How would you get a status code from an image url. 您如何从图片网址获取状态代码。 If the image status code is 404, I'd like to replace it with something else.
如果图像状态码是404,我想用其他东西代替它。
You can use onload
and onerror
events. 您可以使用
onload
和onerror
事件。
<script type="text/javascript">
function onImageLoad() {
alert("imgOne is loaded.");
}
function onImageLoadError() {
alert("imgTwo is not loaded.");
}
</script>
<img src="http://via.placeholder.com/350x150" id="imgOne" onload="onImageLoad(this);" />
<img src="http://via1.placeholder.com/350x150" id="imgTwo" onerror="onImageLoadError(this);" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.