I've tried all manner of methods for accomplishing this and nothing has worked so far. The latest attempt follows. The objective is to call a function that tests for the existence of an image file. If the file exists then that image file is to be displayed. Otherwise, an alternative image file is to be displayed.
So in the body of my page I have a very simple:
<script>
document.write(dispimg());
</script>
Inside the head section is:
function dispimg(){
var imgtagf="<IMG alt='";
var imgtagm="Featured Pet' hspace=0 src='gallery/Featured";
var imgtagb=".JPG' size='medium' border=0>;";
var imgtag="";
if (testExists("gallery/Featured.JPG")) {
alert("file exists");
imgtag=imgtagf+imgtagm+imgtagb; }
else {
alert("file does not exist");
imgtag=imgtagf+"No "+imgtagm+"_None"+imgtagb; }
alert("returning "+imgtag);
return imgtag;
}
function testExists(imagepath) {
alert("running testExists");
req = getreq();
// the following alert never displays
alert("about to run imageExists");
req.onreadystatechange = imageExists();
req.open("head", imagepath, true);
req.send(null);
}
function imageExists() {
alert("running imageExists");
if(req.readyState == 4) {
if(req.status == 200) {
// image exists
nB=false;
}
else if(req.status==404) {
// image doesn't exist
nB=true;
}
else {
// all other error codes will set this
nB=null;
} alert("imageExists is "+nB);
return nB;
}
}
</script>
As you can see, I've used alerts to see what is getting executed. The function testExists fails almost immediately and I don't know why. Also, I'm not sure if I can do a document.write building the img tag string as I have done which would result in that image displaying in the body where the dispimg function is called.
So I could use some help/education. I am quite the newbie so "speak slowly" :).
MJS