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

How to determine if a file exists in jquery

coldplay.xixi
Release: 2020-11-30 15:20:27
Original
3354 people have browsed it

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;
     }
}
Copy after login

Usage

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template