Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查本地HTML文件是否存在,如果存在,则重定向到该文件_Javascript_Html - Fatal编程技术网

Javascript 检查本地HTML文件是否存在,如果存在,则重定向到该文件

Javascript 检查本地HTML文件是否存在,如果存在,则重定向到该文件,javascript,html,Javascript,Html,目标:检查是否存在文件名每天更改的本地文件(file:///C:/Directory/example-YYYYMMDD.html)。如果今天的文件尚不存在,则显示一条消息。如果今天的文件确实存在,请重定向到它 到目前为止我所处的位置:由于我找到了一个示例,我下面的内容将检查文件是否存在,如果不存在,则显示一条消息。但如果该文件确实存在,我还没有弄清楚如何使其重定向 <!DOCTYPE html> <html> <head> <script> //T

目标:检查是否存在文件名每天更改的本地文件(file:///C:/Directory/example-YYYYMMDD.html)。如果今天的文件尚不存在,则显示一条消息。如果今天的文件确实存在,请重定向到它

到目前为止我所处的位置:由于我找到了一个示例,我下面的内容将检查文件是否存在,如果不存在,则显示一条消息。但如果该文件确实存在,我还没有弄清楚如何使其重定向

<!DOCTYPE html>
<html>
<head>
<script> //This makes it possible to call a variable URL that changes with today's date
            var date = new Date();
            var y = date.getFullYear(); //The year (YYYY) part of the filename
            var m = date.getMonth()+1;
            var d = date.getDate();
            if(m < 10){m = '0' + m;} //The month (MM) part of the filename
            if(d < 10){d = '0' + d;} //The day (DD) part of the filename
            var date = y + m + d;
            var redirectURL= 'file:///C:/Directory/example-' + date + '.html'
</script>
</head>

<body>
<p></p>
<span></span>
<script> //This checks for the existence of redirectURL
    function get_error(x){
        document.getElementsByTagName('span')[0].innerHTML+=x+" hasn't been created yet. Go create it manually."; //This is what to display if the file doesn't exist
    }
    url=redirectURL;
    url+="?"+new Date().getTime()+Math.floor(Math.random()*1000000);
    var el=document.createElement('script');
    el.id=redirectURL;
    el.onerror=function(){if(el.onerror)get_error(this.id)} //If the file doesn't exist, trigger the get_error function
    el.src=url;
    document.body.appendChild(el);
</script>
</body>
</html>

//这使得调用随今天日期变化的变量URL成为可能
变量日期=新日期();
var y=date.getFullYear()//文件名的年份(YYYY)部分
var m=date.getMonth()+1;
var d=date.getDate();
如果(m<10){m='0'+m;}//文件名的月份(MM)部分
如果(d<10){d='0'+d;}//文件名的日期(DD)部分
var日期=y+m+d;
var重定向URL=file:///C:/Directory/example-“+date+”.html”

//这将检查重定向URL是否存在 函数get_错误(x){ document.getElementsByTagName('span')[0].innerHTML+=x+“尚未创建。请手动创建它。”;//如果文件不存在,将显示此内容 } url=重定向url; url+=“?”+新日期().getTime()+数学地板(数学随机()*1000000); var el=document.createElement('script'); el.id=重定向URL; el.onerror=function(){if(el.onerror)get_error(this.id)}//如果文件不存在,则触发get_error函数 el.src=url; 文件.正文.附件(el);
您可以使用
fetch
window.location.href

如有:

fetch('https://api.github.com ')//重定向url=https://api.github.com
.then(d=>{console.log('redirecting');
window.location.href=https://api.github.com'})

.catch(e=>console.log('do other thing'))
您可以使用
fetch
window.location.href

如有:

fetch('https://api.github.com ')//重定向url=https://api.github.com
.then(d=>{console.log('redirecting');
window.location.href=https://api.github.com'})

.catch(e=>console.log('do other thing'))
我查看了资源加载,并确定它是一个需要添加的onload:

el.onload = function redirect(){window.location = redirectURL;};
最终结果如下:


//这使得调用随今天日期变化的变量URL成为可能
变量日期=新日期();
var y=date.getFullYear()//文件名的年份(YYYY)部分
var m=date.getMonth()+1;
var d=date.getDate();
如果(m<10){m='0'+m;}//文件名的月份(MM)部分
如果(d<10){d='0'+d;}//文件名的日期(DD)部分
var日期=y+m+d;
var重定向URL=file:///C:/Directory/example-“+date+”.html”

//这将检查重定向URL是否存在 函数get_错误(x){ document.getElementsByTagName('span')[0].innerHTML+=x+“尚未创建。请手动创建它。”;//如果文件不存在,将显示此内容 } url=重定向url; url+=“?”+新日期().getTime()+数学地板(数学随机()*1000000); var el=document.createElement('script'); el.id=重定向URL; el.onerror=function(){if(el.onerror)get_error(this.id)}//如果文件不存在,则触发get_error函数 el.onload=function redirect(){window.location=redirectURL;}//如果文件确实存在,请重定向到变量redirectURL的值 el.src=url; 文件.正文.附件(el);
我阅读了关于资源加载的内容,并确定这是一个需要添加的onload:

el.onload = function redirect(){window.location = redirectURL;};
最终结果如下:


//这使得调用随今天日期变化的变量URL成为可能
变量日期=新日期();
var y=date.getFullYear()//文件名的年份(YYYY)部分
var m=date.getMonth()+1;
var d=date.getDate();
如果(m<10){m='0'+m;}//文件名的月份(MM)部分
如果(d<10){d='0'+d;}//文件名的日期(DD)部分
var日期=y+m+d;
var重定向URL=file:///C:/Directory/example-“+date+”.html”

//这将检查重定向URL是否存在 函数get_错误(x){ document.getElementsByTagName('span')[0].innerHTML+=x+“尚未创建。请手动创建它。”;//如果文件不存在,将显示此内容 } url=重定向url; url+=“?”+新日期().getTime()+数学地板(数学随机()*1000000); var el=document.createElement('script'); el.id=重定向URL; el.onerror=function(){if(el.onerror)get_error(this.id)}//如果文件不存在,则触发get_error函数 el.onload=function redirect(){window.location=redirectURL;}//如果文件确实存在,请重定向到变量redirectURL的值 el.src=url; 文件.正文.附件(el);
对于OP要求的本地资源(即指定了
文件:
协议的URL)无效。对于OP要求的本地资源(即指定了
文件:
协议的URL)无效。