usertest usertest - 1 year ago 277
Javascript Question

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

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

Answer Source

With jQuery:

    error: function()
        //file not exists
    success: function()
        //file exists


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

function UrlExists(url)
    var http = new XMLHttpRequest();'HEAD', url, false);
    return http.status!=404;

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download