Check if file exists using javascript or jQuery

0

How can I check using javascript or jQuery that a file exists?

OBS : I opened the question and I already answered.

    
asked by anonymous 27.08.2015 / 20:07

1 answer

1

Below are the means that I have found.

Javascript

function verificaUrl(url) {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status != 404;
}

jQuery

$.ajax({
    url:'http://www.exemplo.com.br/arquivo.txt',
    type:'HEAD',
    success: function() {
        //arquivo existe
    }
    error: function() {
        //arquivo não existe
    },
 });
    
27.08.2015 / 20:07