关闭警告框后如何重定向页面?
How do i redirect the page after closing an alert box?
我一直在尝试告诉浏览器在用户关闭警告框后将用户重定向到其他位置,但我尝试过的 none 方法似乎有效,所以我想问你是否可以检查我的代码并告诉我你是否看到了满足我需求的可能解决方案。这段代码只是一个练习,我正在练习和测试javascript。
到目前为止我已经尝试使用这些,但没有任何效果。
window.location.href();
window.location.replace();
window.location.assign();
location.href();
location.replace();
location.assign();
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代码:
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);
}
}
您应该尝试以下方法
window.location.href = 'welcome.html';
您应该将 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";
}
希望这对您有所帮助!
实际上你只需要添加 type="button" 到按钮,你的代码就可以工作了:
<button type="button" onclick="login();">Login</button>
原因是按钮在表单中自动充当 type="submit"。
使用此 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);
}
}
我一直在尝试告诉浏览器在用户关闭警告框后将用户重定向到其他位置,但我尝试过的 none 方法似乎有效,所以我想问你是否可以检查我的代码并告诉我你是否看到了满足我需求的可能解决方案。这段代码只是一个练习,我正在练习和测试javascript。
到目前为止我已经尝试使用这些,但没有任何效果。
window.location.href();
window.location.replace();
window.location.assign();
location.href();
location.replace();
location.assign();
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代码:
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);
}
}
您应该尝试以下方法
window.location.href = 'welcome.html';
您应该将 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";
}
希望这对您有所帮助!
实际上你只需要添加 type="button" 到按钮,你的代码就可以工作了:
<button type="button" onclick="login();">Login</button>
原因是按钮在表单中自动充当 type="submit"。
使用此 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);
}
}