I'm trying to check with jQuery if an image exists in my domain for then put them in an array. I have that code:
jQuery(document).ready(function($) {
var images = new Array();
var flag = true;
var i = 0;
var x = 1;
while(flag) {
$.ajax({
url:'http://localhost/testImages/images/picture-1-' + x + '.jpg',
type:'HEAD',
error: function() {
flag = false;
return flag;
},
success: function() {
images[i] = '<?php echo $productSlug . "-1-"; ?>';
images[i] += x + '.jpg';
return images[i];
}
i++;
x++;
});
alert(flag);
}
});
When I run the page I must force close firefox because I had some error with load charges. I have the alert(flag) to know the state of that variable and wait for the false. I tried commenting the while loop and replacing 'x' for a number that does not exists in my images name and the flag variable always returns true.
I think that the problem is in the ajax code but I don't have much experience with it.