1

Goal: Check for the existence of a local file with a filename that changes each day (file:///C:/Directory/example-YYYYMMDD.html). If today's file does not yet exist, display a message. If today's file does exist, redirect to it.

Where I'm at so far: What I have below will check for the existence of the file and display a message if it doesn't exist, thanks to an example I found here. But in the event the file does exist, I haven't figured out how to make it redirect.

<!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>
David
  • 71
  • 6

2 Answers2

2

you can use fetch and window.location.href:

if available :

fetch('https://api.github.com ')//redirect url = https://api.github.com
.then(d=>{console.log('redirecting');
  window.location.href = 'https://api.github.com'})
.catch(e=>console.log('do other thing'))

if not available :

fetch('https://notavailable.com ')//redirect url = https://notavailable.com
.then(d=>{console.log('redirecting');
  window.location.href = 'https://notavailable.com'})
.catch(e=>console.log('do other thing'))
Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
  • that won't work for local resources (i.e. URLs with the `file:` protocol specified) which is what the OP is asking for. – Dan O May 04 '20 at 03:36
1

I read up on resource loading and determined it's an onload that I need to add:

el.onload = function redirect(){window.location = redirectURL;};

Bringing the end result to something like this:

<!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.onload = function redirect(){window.location = redirectURL;}; //If the file does exist, redirect to the value of the variable redirectURL
    el.src=url;
    document.body.appendChild(el);
</script>
</body>
</html>
David
  • 71
  • 6