0

Goal: I currently have two local html files, one that redirects to the other. There are currently no checks in place to ensure the file I'm redirecting to does in fact exist beforehand. I'd like to redirect to the file only if it exists, and display a message if it doesn't exist yet.

Context: An app on my local machine outputs a fresh html file each day with a filename of today's date. Since I like to have this bookmarked but can't bookmark a dynamic url, I created this second html file (with a static filename) and bookmark that instead. This is where I'm at:

<!DOCTYPE html>
<html>
<head>
    <title>Example Title</title>
    <script>
        function url(){
            var date = new Date();

            var y = date.getFullYear();
            var m = date.getMonth()+1;
            var d = date.getDate();

            if(m < 10){m = '0' + m;}
            if(d < 10){d = '0' + d;}

            var date = y + m + d;

            return 'file:///C:/Directory/html/example-' + date + '.html';

        } window.open(url(),"_parent");
    </script>
</head>

<body>
    <p>Redirecting...</p>
</body>
</html>
David
  • 71
  • 6
  • you can use XHR to retrieve the file first and see if it is valid – user120242 May 03 '20 at 05:52
  • https://stackoverflow.com/questions/24296721/how-to-check-if-url-exists-using-javascript – Vinit Desai May 03 '20 at 05:58
  • If anyone else stumbles across this later trying to accomplish the same thing, this was my eventual solution: https://stackoverflow.com/a/61597754/13458321 – David May 06 '20 at 04:18

0 Answers0