如何检查文件是否存在于jQuery或JavaScript? [英] How do I check if file exists in jQuery or JavaScript?
本文介绍了如何检查文件是否存在于jQuery或JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查我的服务器上的文件是否存在于jQuery或JavaScript? >
$。ajax({
url:'http://www.example.com/somefile.ext',
$ {
//文件存在
}
});
编辑:
用于检查404状态,而不使用jQuery
函数UrlExists(url)
{
var http = new的XMLHttpRequest();
http.open('HEAD',url,false);
http.send();
返回http.status!= 404;
$ p 小的改变可以检查状态HTTP状态码200(成功),相反。
How do I check if a file on my server exists in jQuery or JavaScript?
解决方案 With jQuery:
$.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, without 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 status HTTP status code 200 (success), instead.
这篇关于如何检查文件是否存在于jQuery或JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文