PDA

View Full Version : How to put a prompt when there is no image during popup


bekbutatz
Apr 13, 2009, 02:15 AM
Hello! I have a the following code at the head
===
function popUp(popurl){
var winpops=window.open("","LargeImage","location=no,menubar=no,status=no,directories=no,sc rollbars=no,toolbar=no,width=350,height=500");

if (popurl != null) {winpops.document.write("<center><img src= '"+popurl+"' >"); }
else {
winpops.document.write("<BR><BR><BR>");

winpops.document.write("<br><center><img src= '"+popurl+"' alt='No Image Available at this Time' >");
}

winpops.document.write("<BR><BR><BR>");
winpops.document.write("<input type='button' value=' Close Window ' onclick='javascript:window.close()'>");
}
===

and at the body
<A HREF="javascript:popUp('computer.jpg')"><img src="computer.jpg"></a></TD>

How do I put a message that tells if that image is not available? Thanks

jstrike
May 26, 2009, 02:17 PM
This should do the trick but you'll need to fully test it...

<script>
function isImageAvailable(imgPath) {
var img = new Image();
img.src=imgPath;
return img.height>0;
}


if(!isImageAvailable("/images/SomeImage.png"))
alert("That image is not available");
</script>

The only problem with doing it this way is that if the image is large the JavaScript will load the entire image to check it. You might want to adapt it to make it more efficient.