【问题标题】:If else, then redirect如果是,则重定向
【发布时间】:2020-05-29 20:14:43
【问题描述】:

我正在尝试创建一个密码登录页面,但是当您输入正确的密码时,它只会提醒用户它是正确的。但我希望它发出警报并重定向到另一个页面。例如,google.com。这是我的代码,任何帮助将不胜感激。

// Function to check Whether both passwords 
// are same or not. 
function checkPassword(form) {
    password = form.pass.value;
    correct = "admin";

    // If password not entered 
    if (password == '')
        alert("Please enter Password");

    // If confirm password not entered 
    else if (correct == '')
        alert("Please enter confirm password");

    // If Not same return False.     
    else if (password != correct) {
        alert("\nPassword is incorrect. Please try again...")
        return false;
    }

    // If same return True. 
    else {
        alert("Password is correct, press okay to continue.")
        return true;
    }
}

我对编码非常陌生,是从 YouTube 视频中获得的,所以我对 JavaScript 几乎一无所知,但我对 HTML 和 CSS 有所了解。

【问题讨论】:

标签: javascript authentication passwords


【解决方案1】:

使用 window.location.href 指向您要继续访问的网址。如果密码正确,我已修改您的示例以重定向到 www.google.com

// Function to check Whether both passwords 
// are same or not. 
function checkPassword(form) {
  password = form.pass.value;
  correct = "admin";

  // If password not entered 
  if (password == '')
    alert("Please enter Password");

  // If confirm password not entered 
  else if (correct == '')
    alert("Please enter confirm password");

  // If Not same return False.     
  else if (password != correct) {
    alert("\nPassword is incorrect. Please try again...")
    return false;
  }

  // If same return True. 
  else {
    alert("Password is correct, press okay to continue.")
    window.location.href = 'https://www.google.com'
  }
}

【讨论】:

  • 它似乎不起作用。仅当它托管在服务器上时才有效?因为我是通过谷歌浏览器查看的。
猜你喜欢
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
相关资源
最近更新 更多