mcgrailm mcgrailm - 21 days ago 5
Javascript Question

jquery xml exists

I want to use javascript / jquery to determine if an xml file exists.
I don't need to process it; I just need to know whether it's available or not,but I can't seem to find a simple check.

Here is what I've tried:

jQuery.noConflict();

jQuery(document).ready(function(){
var photo = '223';
var exists = false;

jQuery.load('/'+photo+'.xml', function (response, status, req) {
if (status == "success") {
exists = true;
}
});
});

Answer

Assuming you are talking about an xml file on the server, you could do a ajax request and then write a custom error handler to check the error response message. You'll need to know what the exact error message code is for a missing file (usually 404). You can use Firebug Console to check what the exact error message and code is.

$.ajax({
     type: "GET",
     url: "text.xml",
     dataType: "xml",
     success: function(xml) {
        alert("great success");
     }, 
     error: function(xhr, status, error) {
        if(xhr.status == 404)
        {
            alert("xml file not found");
        } else {
            //some other error occured, statusText will give you the error message
            alert("error: " + xhr.statusText);
        }
     } //end error
 }); //close $.ajax(