javascript 判断文件是否存在
<title>常用下载</title>
<script type="text/javascript" >
//判断服务器上的文件(网络文件是否存在)
function getURL()
{
var objDiv=document.getElementById ("show");
var objs=objDiv.childNodes;
for(var i=0;i<objs.length;i++)
{
if(objs .tagName=="A")
{
var objA=objs;
var url=objA.href;
var xmlhttp =new ActiveXObject("microsoft.XMLHTTP");
xmlhttp.open("HEAD",url,false);
xmlhttp.send();
if(xmlhttp.readyState==4)
if(xmlhttp.Status==404)
{
objA.href="javascript:alert('该文件已过期,不能下载!')";
objA.innerText+=",该文件已过期,不能下载!";
}
}
}
}
//判断客户端文件是否存在
var fso,s=filespec; // filespec="C:/path/myfile.txt"
fso=new ActiveXObject("Scripting.FileSystemObject");
if(fso.FileExists(filespec))
s+=" exists.";
else
s+=" doesn't exist.";
alert(s);
</script>
</head>
<body onload ="getURL()">
<div class="smalltoolbar" style="height: 40px; font-size :larger; color :Green ">
<p style ="padding-left :10px; padding-top :5px"> 工具常用下载</p>
</div>
<div id ="show" style ="padding-left :10px; padding-top :2px">
☆<a id="B" href="downloads/xx.rar">软件1</a>
<br/>☆<a id="a" href="downloads/cc.doc" >vv说明书</a>
</div>
<script type="text/javascript" >
//判断服务器上的文件(网络文件是否存在)
function getURL()
{
var objDiv=document.getElementById ("show");
var objs=objDiv.childNodes;
for(var i=0;i<objs.length;i++)
{
if(objs .tagName=="A")
{
var objA=objs;
var url=objA.href;
var xmlhttp =new ActiveXObject("microsoft.XMLHTTP");
xmlhttp.open("HEAD",url,false);
xmlhttp.send();
if(xmlhttp.readyState==4)
if(xmlhttp.Status==404)
{
objA.href="javascript:alert('该文件已过期,不能下载!')";
objA.innerText+=",该文件已过期,不能下载!";
}
}
}
}
//判断客户端文件是否存在
var fso,s=filespec; // filespec="C:/path/myfile.txt"
fso=new ActiveXObject("Scripting.FileSystemObject");
if(fso.FileExists(filespec))
s+=" exists.";
else
s+=" doesn't exist.";
alert(s);
</script>
</head>
<body onload ="getURL()">
<div class="smalltoolbar" style="height: 40px; font-size :larger; color :Green ">
<p style ="padding-left :10px; padding-top :5px"> 工具常用下载</p>
</div>
<div id ="show" style ="padding-left :10px; padding-top :2px">
☆<a id="B" href="downloads/xx.rar">软件1</a>
<br/>☆<a id="a" href="downloads/cc.doc" >vv说明书</a>
</div>