-2

I have a link of a website stored in a .txt file, I want it so whenever I visit my site it will auto redirect to the link in the .txt file. Is this possible with HTML? If not, I am happy with another coding language. Thank you!

2 Answers2

1

Not in pure HTML, however you can achieve this with some javascript for example -

<html>
<head>
    <script type="text/javascript">
        function redirectToUrl() {
            var rawFile = new XMLHttpRequest();
            rawFile.open("GET", "http://YourServer/yourtextfile.txt", true);
            rawFile.onreadystatechange = function () {
                if (rawFile.readyState === 4) {
                    var url = rawFile.responseText;
                    document.location = url;
                }
            }

            rawFile.send();
        }
        redirectToUrl();
    </script>
</head>

<body>
    Redirecting!
</body>
</html>
CD-jS
  • 1,125
  • 1
  • 15
  • 32
  • Hi, when it comes to this line "rawFile.open("GET", "http://YourServer/yourtextfile.txt", true);" Can I replace "http://youserver" with the name of the text file? So it would be "rawFile.open("GET", "link.txt", true);" The .txt file is in the same directory as the javascript code. – Diamond Slashes Aug 30 '17 at 12:04
  • Yes, thats correct. Though if you're trying it locally you might run into some cross-origin request errors. – CD-jS Aug 30 '17 at 12:06
0

No, it is not possible in pure HTML.

However, you can do it very easily in Javascript included in your HTML page. This is a pretty clear example of how to read in a text file in Javascript.

Louis 'LYRO' Dupont
  • 1,052
  • 4
  • 15
  • 35