samedi 22 décembre 2018

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.

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>
<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:

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

Aucun commentaire:

Enregistrer un commentaire