I'm using ajax to check to see if there is a file available in my image directory. If it is there, I append it to my html code, and change a boolean value to true, letting the user know it was found. If it does not find the file name, I check if it is a 2 part image (concating the numbers 1 and 2 at the end of the image name and producing 2 images). The boolean value is also changed to true saying that there were images found. Although if there is no single or two-part image, the code should just error out of the ajax, leaving the boolean as false, telling the user there are no images.
My logic is working great on everything except for notifying the user when there isn't an image. In the ajax success function, the images always append (whether success or error), but the boolean value never changes after success. Here is the code I'm running.
boolean = false //declared globally
function tableSelect(location){
$("#popupPhoto").text("");
var part = $("#table"+location).text().split(" ");
//part[0] always empty
for(var i=1;i<part.length;i++){
//change default value to false
boolean = false;
/* original code
imageExists(part[i]);
//when no file... is found, boolean stays false, causing this if statement to function
if(boolean == false){
alert("No Information for "+imagename+" of "+part[i]);
//displayes image that says "No Information Available"
$("#popupPhoto").append($("<img>", {src: 'images/imagedir/nia.png', "class": 'popphoto', alt: 'NIA' }));
}
*/
//new code
imageExists(part[i]).fail(function () {
alert("No Information for "+imagename+" of "+part[i]);
$("#popupPhoto").append($("<img>", {src: 'images/imagedir/nia.png', "class": 'popphoto', alt: 'NIA' }));
})
}//Displays images in popup
$("#popupPhoto").addClass("ui-popup-active");}
Here is the function that checks for the image
function imageExists(part){
var url = "images/imagedir/"+part+".png";
$.ajax({
url:url,
type:'HEAD',
error: function()
{
$("#popupPhoto").append($("<img>", {src: 'images/imagedir/'+part+'1.png', "class": 'popphoto', alt: part.concat("1") }));
$("#popupPhoto").append($("<img>", {src: 'images/imagedir/'+part+'2.png', "class": 'popphoto', alt: part.concat("2") }));
//boolean = true;
//boolean changes here
},success: function()
{//boolean will not change here
//boolean = true;
$("#popupPhoto").append($("<img>", {src: 'images/imagedir/'+part+'.png', "class": 'popphoto', alt: part }));
}
});
}
I understand this is probably me not understanding the details of this function and how it works, but if anyone could help get around this issue or suggest a better way to do it, that is essentially what I am looking for. Thanks in advance.