简体   繁体   中英

window.location.replace() not working in a form

I'm trying to set up a login form who redirects the user to another page after submitting the form. However neither window.location.replace() , window.location.href , or window.open() seem to work and I can't figure out why. I checked on the developer tools and it gives me no error.

Here's the javascript:

function loginUtente(){

    var email = document.getElementById('loginemail').value;
    var password = document.getElementById('loginpassword').value;

    var utente = localStorage.getItem(email);

    if( utente != null && JSON.parse(utente).password == password){
       window.alert("login effettuato!");
        window.location.replace("http:/www.google.it");
    }

    else{
        window.alert("Utente o password non corretti");
    }

    return false;

}

And here's the HTML:

        <form class="form-group" id="formlogin" onsubmit="loginUtente()">

            <label>Email</label>
        <div class="input-group">
            <div class="input-group-addon"><span class="glyphicon glyphicon-user"></span></div>
    <input id="loginemail" type="text" name="nome" class="form-control" placeholder="Indirizzo Email" required ></input>
        </div>

        <br>

        <label>Password</label>
    <div class="input-group">
         <div class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></div>
    <input id="loginpassword" type="password" name="password" class="form-control" placeholder="Password" required>
    </div>

    </div>

    <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="submit" class="btn btn-success" ><span class="glyphicon glyphicon-send" ></span> Login</button>


        </form>

Putting the return false inside the if is not working either. The Google url is of course a placeholder url.

In the form tag you missed to add return statement :

 <form class="form-group" id="formlogin" onsubmit="return loginUtente()">

So, form is submitted without waiting for response of function. and function code is not executed.

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