简体   繁体   中英

redirect page based on user input

I'm trying to do a very simple thing, but I can't. I would like to be redirected to a specific page depending on the input value entered by the user on pressing the enter key. However I can't and every time I am redirected to the same page. What am I doing wrong? this is the code

    window.onkeyup = keyup;


var inputTextValue;

function keyup(e) {
  //setting your input text to the global Javascript Variable for every key press
  inputTextValue = e.target.value;
  console.log(inputTextValue);
 
  if (e.keyCode == 13) {
    if (inputTextValue == "myInput") {
      return true;
      //   window.location = "http://www.google.it";
    } else {
      return false;
      //   window.location = "http://www.facebook.it";
    }
  }
}

I used window.location.href property to change the url of page

 document.querySelector("#btn").addEventListener("click", () => { const text = document.querySelector("#inputRedirectText").value; if (text === "myName") { window.location.href = "https://www.google.com"; } else { window.location.href = "https://www.facebook.com"; } });
 <input type="text" id="inputRedirectText" placeholder="type..."/> <button id="btn">Done</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM