-1

I'm working on a simple login page. When the user types in the correct username and password it redirects them to another page. So far I have tried:

window.location, window.location.replace window.href and window.open the only one that works is window.open, however I'm trying to make is so it does not open a newtab, it just redirects them to another page.

Html

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="master.css">
  <title>Day Four</title>
</head>
<body>
 <h1>Log In</h1>

 <form onsubmit="valid()">
 <label for="username">Username:</label>
 <input id = 'user-name' type="text" name="" value="" required>

 <label for="password">Password:</label>
 <input id = 'pass-word' type="password" name="" value="" required>

 <input id = 'log-in'type="submit" name="" value="Log In">
 </form>

 </div>

 </body>
 <script src="mainJs.js" charset="utf-8"></script>
 </html>

JS

function valid(){
 var username = document.querySelector('#user-name').value;
 var password = document.querySelector('#pass-word').value;

 if(username === 'test' && password === '1'){
   window.open("gradebook.html")

 }else{
   alert("Wrong username or password")
 }

}

Edit: I know it's not secure. I put the code that works window.open("gradebook.html") seeing how the other ones don't do anything. I also don't get any errors in the console when using the other ones.

Paul
  • 43
  • 1
  • 1
  • 5
  • We can't tell you what you are doing wrong when you list three things that don't work and one that does … but only show us how you use the one that works. – Quentin Apr 16 '17 at 21:58
  • `for="username"` and `id = 'user-name'` don't match. Use [a validator](https://validator.w3.org/nu/). – Quentin Apr 16 '17 at 21:58
  • http://stackoverflow.com/a/506004/7733724 – Sam Apr 16 '17 at 21:59
  • @Quentin Yea those were just some other things I was testing out before the on submit, I was trying to use an event listener with the submit button – Paul Apr 16 '17 at 22:02
  • Were you trying `window.location('gradebook.html')`? Did you check your console for errors? – Ruan Mendes Apr 16 '17 at 22:04
  • what's wrong with using `window.open('gradebook.html', '_self')` ? – yezzz Apr 16 '17 at 22:10
  • @yezzz What's wrong is that there is a correct way to do what the OP was trying, no need for a workaround because you couldn't figure out the problem with the more straight forward way. – Ruan Mendes Apr 16 '17 at 22:13
  • The console has no errors and @yezzz window.open('gradebook.html', '_self') does not work. – Paul Apr 16 '17 at 22:18
  • Juan, I was not giving an answer now was I? Of course I would use window.location, and never even tried using window.open (.., "_self"), but because OP mentioned window.open, and while some knowledgeable people are in the thread I thought I'd ask if anything's wrong with it. @Paul thank you for answering that. – yezzz Apr 16 '17 at 22:36
  • @Juan Mendes I'm not sure why but if I take out the form tags everything seems to work fine. – Paul Apr 17 '17 at 00:55

2 Answers2

0

It looks like the problem was with the form tags. If I remove the form tags everything starts working as it should.

Paul
  • 43
  • 1
  • 1
  • 5
-1

All you need to do is set window.location to the new URL.

document.querySelector("button").addEventListener("click", function(){
  window.location = "http://cnn.com";
});
<button>Click Me</button>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • 1
    Not the downvoter, but the OP says they tried that. I know... the OP didn't show what they tried – Ruan Mendes Apr 16 '17 at 22:02
  • @JuanMendes The OP didn't try it correctly. This is the correct answer. – Scott Marcus Apr 16 '17 at 22:03
  • Yeah, maybe the OP tried `window.location('gradebook.html')` – Ruan Mendes Apr 16 '17 at 22:05
  • @ScottMarcus I tried that already it didn't work, Because none of them worked I posted the one that worked in the code. I got no errors in the console when using the other ones but it still didn't work. – Paul Apr 16 '17 at 22:25
  • @Paul you say it didn't work, but you didn't post the exact code you tried. My answer is correct. You must not have done what I'm showing or you have additional code that is causing an error. – Scott Marcus Apr 17 '17 at 00:00