简体   繁体   English

关闭警报框后如何重定向页面?

[英]How do i redirect the page after closing an alert box?

I've been trying to tell the browser to redirect the user to a different location after they close the alert box but none of the methods i have tried seems to be working, so i'm asking you if you can check my code and tell me if you see a possible solution to my needs. 我一直试图告诉浏览器,在用户关闭警报框后将其重定向到其他位置,但是我尝试过的所有方法似乎都没有用,所以我问你是否可以检查我的代码并告诉我,如果您认为有可能解决我的需求。 This code is just an exercise, I'm practicing and testing javascript. 这段代码只是一个练习,我正在练习和测试javascript。

So far i have tried using these, but nothing's worked. 到目前为止,我已经尝试使用这些功能,但是没有任何效果。

window.location.href(); 
window.location.replace(); 
window.location.assign(); 
location.href(); 
location.replace(); 
location.assign();

HTML code: HTML代码:

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="javascript.js"></script>
</head>
<body>
<div id="container" align="center"><br />
<form>
Nickname: <input type="text" id="nickname" name="nickname"><br />
Password: <input type="password" id="password" name="password"><br />
<button onclick="login();">Login</button>
</form>
<p id="result:"></p><br />
</div>
</body>
</html>

Javascript Code: JavaScript代码:

function login(){
    var nickname = document.getElementById("nickname").value;
    var password = document.getElementById("password").value;
    var result = document.getElementById("result");
    var error = "Invalid Credentials!";
    var sucess = "Login Sucess!";

    if (nickname == "neo", password == 123){
        alert(sucess);
        location.assign("welcome.html");
    }
    else if(nickname == 22){
        confirm("Awesome!");
        location.replace("welcome.html");
    }
    else if(nickname == ""){
        alert("Nickname is required!");
    }
    else{
        alert(error);
    }
}

You should be using window.location.href as an assignment (using = ) rather than treating it as a function (using () ): 您应该将window.location.href用作赋值(使用= ),而不是将其视为函数(使用() ):

if (nickname == "neo", password == 123){
    alert(sucess);
    window.location.href = "welcome.html";
}
else if(nickname == 22){
    confirm("Awesome!");
    window.location.href = "welcome.html";
}

Hopefully this helps! 希望这会有所帮助!

您应该尝试以下

window.location.href = 'welcome.html';

Actually you just need to add type="button" to the button and your code will work: 实际上,您只需要在按钮上添加type =“ button”,您的代码就可以使用:

<button type="button" onclick="login();">Login</button>

The reason is that a button act automatically as type="submit" inside a form. 原因是按钮自动在表单内充当type =“ submit”。

use this JavaScript code: 使用以下JavaScript代码:

 function login(){ var nickname = document.getElementById("nickname").value; var password = document.getElementById("password").value; var result = document.getElementById("result"); var error = "Invalid Credentials!"; var sucess = "Login Sucess!"; if (nickname == "neo", password == 123){ if (alert(sucess)) {} else { window.location = "welcome.html"; } } else if(nickname == 22){ if (confirm("Awesome!")) { window.location = "welcome.html"; } } else if(nickname == ""){ alert("Nickname is required!"); } else{ alert(error); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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