user8007596
user8007596

Reputation:

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

Upvotes: 0

Views: 681

Answers (4)

Gidyyy
Gidyyy

Reputation: 103

use this 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){

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

Upvotes: 0

A. Meshu
A. Meshu

Reputation: 4148

Actually you just need to add type="button" to the button and your code will work:

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

The reason is that a button act automatically as type="submit" inside a form.

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44087

You should be using window.location.href as an assignment (using =) rather than treating it as a function (using ()):

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!

Upvotes: 2

Jayant Varshney
Jayant Varshney

Reputation: 1825

You should try the following

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

Upvotes: 0

Related Questions