简体   繁体   中英

Html page does not load (Javascript)

I've been trying to switch pages from one login page to another. It does not seem to load or am i doing something wrong?

the link of code pen: https://codepen.io/batajusasmit/pen/OJVyGEW

Here is my code.

 function validate() {
     var username = document.getElementById('username').value;
     var password = document.getElementById('password').value;

         if (username == 'admin' && password == 'admin') {
             alert('login successful!');
             window.location = 'link.html';
             return false;
            }
          }

You have to stop submission on same page and then go to new location

You have used validate() function in button onclick . But you should use return validate() which will return boolean value to stop form from submitting

 function validate() { var username = document.getElementById('username').value; var password = document.getElementById('password').value; if (username == 'admin' && password == 'admin') { alert('login successful!'); window.location = '/'; //change with your location return false; } }
 <div class="container"> <form> <h1 id="form">Login!</h1> <div class="login"> <label class="name">username:</label> <input id="username" class="username" type="text" placeholder="name..."> <label class="pass">password:</label> <input id="password" class="password" type="password" placeholder="password..."> </div> <div class="submit"> <button id="submit" class="btn btn-primary" onclick="return validate()">Submit</button> </div> </form> </div>

I have figured it out. I just needed the backslash.

 window.location.href = '/link.html'; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM