var flag
var xmlhttp
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest()//其他浏览器
}
else if (window.ActiveXObject)
{
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")//旧版IE
}
catch (e) { }
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")//新版IE
}
catch (e) { }
if (!xmlhttp) {
window.alert("不能创建XMLHttpRequest对象")
}
}
xmlhttp.open("GET",yourFileURL,false)
xmlhttp.send()
if(xmlhttp.readyState==4){
if(xmlhttp.status==200)
flag =true
else
flag =false
}
return flag
},
<script type="text/javascript">function test() {
var fileURL = "" //文件路径(相对路径)
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
xmlhttp.open("GET",fileURL,false)
xmlhttp.send()()
if(xmlhttp.readyState==4){
if(xmlhttp.status==200) return true //存在
else if(xmlhttp.status==404) return false //不存在
else alert("Error") //报错
//其他状态
}
}
</script>
判断客户端文件时,可以用var fso,s=filespec// filespec="C:/path/myfile.txt"
fso=new ActiveXObject("Scripting.FileSystemObject")
if(fso.FileExists(filespec))
s+=" 文件存在."
else
s+=" 文件不存在."
alert(s)
判断服务器端(网络文件)时,可以用
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
xmlhttp.open("GET",yourFileURL,false)
xmlhttp.send()
if(xmlhttp.readyState==4){
if(xmlhttp.status==200)s+=" 存在."//url存在
else if(xmlhttp.status==404)s+=" 不存在."//url不存在
else s+=""//其他状态
}
alert(s)