利用javascript判斷文件是否存在


1 判斷本地文件是否存在

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);

  2.判斷網絡上文件是否存在

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對象");    
    }    
}    
yourFileURL="https://winycg.github.io/"+textSearch.value+".html"  
xmlhttp.open("GET",yourFileURL,false);  
xmlhttp.send();  
if(xmlhttp.readyState==4){     
    if(xmlhttp.status==200)   
        window.location = yourFileURL; //url存在     
    else 
        alert("該視頻名不存在"); //url不存在     
}   

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM