2

I am trying to make a login using js, I know this isn't secure it's just for school - they require it.

If username and password are correct I want it to open my photos page, this isn't happening.

As far as I can tell everything else is working just fine, it collects and compares data correctly, but won't open the new page. I have tried variations on the this.location.href such as window.location and location.replace.

I have also tried changing the URL using full URL and photos.php

The function is called from a click within the HTML :

<button type="submit" onclick="checkPassword()" class="button">Login</button>

This is the JavaScript:

function check() {   //onclick
    var username = "test";
    var password = "admin";
    var usernameEntered = document.getElementById("username").value;
    var passwordEntered = document.getElementById("password").value;

    if (usernameEntered == username && passwordEntered == password) {
        this.location.href = "http://localhost/photos-v4/photos.php";
    } else {
        alert("Incorrect username or password");
    }
}
6
  • 5
    window.location.href Commented Jun 19, 2018 at 7:29
  • When you say it won't work isn't there an error in console that says "cannot read property location of undefined" ? Commented Jun 19, 2018 at 7:29
  • See the above comments and also look into window.open('http://www.google.com'); Commented Jun 19, 2018 at 7:30
  • @Adelin No, it just refreshes the page Commented Jun 19, 2018 at 7:31
  • @GeorgeBailey that's not working neither is window.location.href Commented Jun 19, 2018 at 7:34

2 Answers 2

1

Here a some ways to do it

Open in new page

window.open('http://localhost/photos-v4/photos.php', '_blank').location;

Open in the current tag

window.location.href = 'http://localhost/photos-v4/photos.php'
Sign up to request clarification or add additional context in comments.

2 Comments

window.open() works but it opens in a new tab, even without '_blank'. Is there a way I can open it in the same tab?
Use window.location.href
0

The problem is in the HTML.

type = "submit" is used to send the data to a new place meaning that the data isn't being captured by the JavaScript.

A solution to this is replacing submit with resetwhich clears the form and, lets the js get the data it needs.

You can read more about HTML buttons here: https://html.com/attributes/button-type/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.