Skip to content
Advertisement

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

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

Advertisement

Answer

With jQuery:

JavaScript

EDIT:

Here is the code for checking 404 status, without using jQuery

JavaScript

Small changes and it could check for status HTTP status code 200 (success), instead.

EDIT 2: Since sync XMLHttpRequest is deprecated, you can add a utility method like this to do it async:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement