How do I check if a file on my server exists in jQuery or Javascript?
Thanks in advance.
How do I check if a file on my server exists in jQuery or Javascript?
Thanks in advance.
$.ajax({
url:'http://www.example.com/somefile.ext',
type:'HEAD',
error: function()
{
//file not exists
},
success: function()
{
//file exists
}
});
EDIT:
Here is the code for checking 404 status, whitout using jquery
function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
Small changes and it could check for error 200 (file exists) etc.
PS. sorry for how it looks, dont know how to paste code, so i use "pre"
Not sure its possible without something serverside going on. It would cause interesting security problems if I could write stuff to interact with your files in javascript. Use PHP for this in my opinion.
What you'd have to do is send a request to the server for it to do the check, and then send back the result to you.
What type of server are you trying to communicate with? You may need to write a small service to respond to the request.