user6369806
user6369806

Reputation:

window.location.href not working

This is my script:

<script type="text/javascript">
    $(document).ready(function(){
        $("#login").click(function(){
            var user = $('#userID').val(); 
            var pass = $('#password').val();
            if (user === '' || pass === ''){
                alert("Please Fill Required Fields");
            } 
            else {
                alert (user+" " +pass);
                var x="<?php confirm();?>";
                alert (x);
                window.location.href = 'http://localhost/Annapoorna/Welcome_Page.php';
            }
        });
    });
</script>

And my HTML

<form class="login-form"> 
    <input type="text" placeholder="username" name="userID" id="userID"/>
    <input type="password" placeholder="password" id="password"/>
    <button id="login" name="login" value="login">Login</button>
</form>

The alerts are coming right but window.location.href is not redirecting to the specified page. Why?

Upvotes: 2

Views: 11643

Answers (2)

Randy
Randy

Reputation: 9849

Why it doesn't work

Display the href (URL) of the current page:

document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;

Result is:

Page location is http://www.yoururl.com/yourlocation


To make it work

To redirect to another page, you can use:

window.location="http://www.yoursite.com";

expand for working example

function Redirect() {
  window.location="http://www.yoursite.com";
}
    <html>
       <head>
                    
       </head>
       
       <body>
          <p>Click the following button, you will be redirected to home page.</p>
          
          <form>
             <input type="button" value="Redirect Me" onclick="Redirect();" />
          </form>
          
       </body>
    </html>

Upvotes: 2

Jai
Jai

Reputation: 74738

Add a type to the button:

    <button id="login" name="login" type="button">Login</button> 

value attribute doesn't seem to be correct on this HTMLButtonElement.

Why?

Seems to me, form getting submitted at same page before navigation.

Upvotes: 1

Related Questions