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