How can i make this little function "imageExists" return wether the ajax request was successful or not?
function imageExists(path){
$.ajax({
url: path,
type: 'HEAD',
error:
function(){
return false;
},
success:
function(){
return true;
}
});
}
Create a var to hold the response on a global level, then set async to false. You need to then set that var in the success or error function.
http://api.jquery.com/jQuery.ajax/
I believe you'll have to use synchronous mode and use a separate a variable for storing the return value.
You're going to be returning the result of the AJAX call either in the
success
function or in theerror
function, so whatever function should be called next should be called from that point.Because an AJAX request is asynchronous (I'd advise not making it a blocking synchronous call), you make the request but don't know when it will return; it'll call you when it returns, so you just need to provide it something to call.