How do I check if file exists in jQuery or pure JavaScript?

前端 未结 18 2127
闹比i
闹比i 2020-11-22 00:56

How do I check if a file on my server exists in jQuery or pure JavaScript?

18条回答
  •  故里飘歌
    2020-11-22 01:37

    I wanted a function that would return a boolean, I encountered problems related to closure and asynchronicity. I solved this way:

    checkFileExistence= function (file){
        result=false;
        jQuery.ajaxSetup({async:false});
        $.get(file)
            .done(function() {
               result=true;
            })
            .fail(function() {
               result=false;
            })
        jQuery.ajaxSetup({async:true});
        return(result);
    },
    

提交回复
热议问题