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.
1 Answer
You can use onload and onerror events.
<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);" />
1 Comment
MPaul
The only issue here is that you can't specifically associate it to a response code.