You can do this with an ajax request, when you get a response from the server, check the response status , example javascript / jQuery >:
function fileExists(urlToCheck){ var fileFound = true; $.ajax({ url: urlToCheck, type: "GET", success: function(data, textStatus, xhr) { console.log(xhr.status); if(xhr.status == 404) fileFound= false; }, complete: function(xhr, textStatus) { console.log(xhr.status); if(xhr.status == 404) fileFound= false; } }); return fileFound; }
I think that this function can be improved much more, given the different HTTP response codes, the codes differ from 404, but this does not mean that the file exists on the server.
source share