zoukankan      html  css  js  c++  java
  • 利用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不存在     
    }   
    

      

  • 相关阅读:
    操作集锦
    大吉大利(算贡献)
    96. 奇怪的汉诺塔
    P760通天之汉诺塔
    First Last Sorting
    9.12小结
    day_06
    day_05作业
    day_05
    day_04
  • 原文地址:https://www.cnblogs.com/remember-forget/p/9989787.html
Copyright © 2011-2022 走看看