Home  >  Article  >  Web Front-end  >  How to determine if a file exists in jquery

How to determine if a file exists in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-25 16:15:493138browse

The jquery method to determine whether a file exists: first encapsulate a fileExists method and receive url parameters; then use the [$.ajax()] method to send a request, set a synchronous request; and finally return the request result.

How to determine if a file exists in jquery

The operating environment of this tutorial: Windows 7 system, jquery version 3.2.1. This method is suitable for all brands of computers.

Related recommendations: "jQuery Video Tutorial"

Jquery method to determine whether a file exists:

js or jquery judgment Whether the file exists, you need to use ajax

Steps:

1. Encapsulate a fileExists method and receive url parameters;

2. Use $.ajax() Method to send a request and set the synchronous mode request;

3. Return the request result.

function fileExists(url) {
     var isExists;
     $.ajax({
         url: url,
         async: false,
         type: 'HEAD',
         error: function () {
             isExists = 0;
         },
         success: function () {
             isExists = 1;
         }
     });
     if (isExists == 1) {
         return true;
     } else {
         return false;
     }
}

Usage

var Exists=Exists('/cache/view_num/' + uid + '.txt');
if(Exists){
    alert('存在')
}

For more programming-related knowledge, please visit: Programming Learning! !

The above is the detailed content of How to determine if a file exists in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn