0

I have a password-protected button that will redirect a user to a specific URL when the password is entered. It works fine but what I want to do is depending on what password the user enters, they will be taken to a URL which corresponds to what they entered. Let's say 'test' was entered in the box, this will redirect users to 'test.com/test/test.html' but if the user entered 'test1' then they'll be redirected to 'test.com/test1/test1.html'. Is there any way to do this? This is my code:

<body>
  <div id="title">
    <span>WELCOME TO</span> <span style="font-size:80px"><br>
        AiZen</span>
  </div>
  <div class="button_container">
    <button class="btn" id="HyperLink1" onclick="location.href='home.html';return ValidatePassword()"><span>Enter Now</span></button>
  </div>
  <script>
    function ValidatePassword() {
      var a = prompt("Enter the password. You know it right?", "");
      if (a == "test") {
        return true
      } else {
        alert("No match. Try again.")
      }
      return false
    }
  </script>
</body>
Rana
  • 2,500
  • 2
  • 7
  • 28
Zenchaeus
  • 3
  • 1
  • Does this answer your question? [How do I redirect to another webpage?](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) – sinsedrix Dec 20 '21 at 16:30

1 Answers1

0

Assuming that you don't have access to a backend app server, or that you don't want to use it for this, you can do something like

document.getElementById("name").addEventListener("input", function(e) {
      var val = e.target.value;
      var form = e.target.closest("form");
    //test.com/test1/test1.html
      form.action = "http://test.com/" + val + "/" + val + ".html";
      console.log("action: " + form.action);
    });
<form>
    <input name="name" id="name" />
    <input type="submit">
    </form>
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • apologies since i'm just a beginner to coding. but what if i wanted to redirect a user to 'test.com/test/test.html' using the password '1234' and redirect them to 'test.com/test1/test1.html' using the password 'tryme'. how would i do this? – Zenchaeus Dec 20 '21 at 16:40
  • @Zenchaeus I have answered your question as you have stated it here. It is absolutely possible to do this new case you have brought up. But you should think about how to do it for a day or two, and if you can't figure it out (I think you'll be able to figure it out), then come back to SO – ControlAltDel Dec 20 '21 at 16:44