簡體   English   中英

關閉警報框后如何重定向頁面?

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

我一直試圖告訴瀏覽器,在用戶關閉警報框后將其重定向到其他位置,但是我嘗試過的所有方法似乎都沒有用,所以我問你是否可以檢查我的代碼並告訴我,如果您認為有可能解決我的需求。 這段代碼只是一個練習,我正在練習和測試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用作賦值(使用= ),而不是將其視為函數(使用() ):

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

希望這會有所幫助!

您應該嘗試以下

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); } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM