window.location.href is not redirecting html page

Manish Bansal

I am trying to build a form and do some validation using JS. On success I want to redirect it to some other page in the same directory. While running window.location.href over console it is working...but while submitting form, it is not working.

Can someone please help me in this.

My code is:

<html>
<head>
<style>
body {font-family: Arial, Helvetica, sans-serif;}

input[type=text], input[type=password] {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  box-sizing: border-box;
}

button {
  background-color: #4CAF50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
}

button:hover {
  opacity: 0.8;
}

.container {
  padding: 16px;
  width: 400px;
  align-self: center;
}

span.psw {
  padding-top: 16px;
}

</style>
<title>Login Page</title>
</head>
<body style="text-align: center;">
<h2 style="text-align: center;">The Mega Cart</h2>

<form name=loginForm onsubmit="login()" style="display: inline-block;">
 

  <div class="container" style="text-align: center;">
    <label for="uname"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="uname" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" required>
        
    <button type="submit">Login</button>
   
  </div>
    <span class="psw">New user Sign up here. <a href="#">Sign Up</a></span>
  </div>
 
</form>

</body>
<script>
    function login() {
      let uname = document.forms["loginForm"]["uname"].value;
      let pwd = document.forms["loginForm"]["psw"].value;
      window.location.href="main.html";
      return false;
    }
    </script>
</html>

I have already checked that this login function is called.

Maulik Savaliya

When you click on the form submit button it call the function login in javascript. In which you have write down the code to redirect the page.

You forgot to prevent the form to submit and that is why you are redirecting to the same page with filled-up values.

If you want to submit your form to specific URL then you should add the action attribute to the form like below.

<form name=loginForm action="URL on which you want to submit form" onsubmit="login()" style="display: inline-block;">

If you want to redirect from the login() function then you should pass the event as an argument from the form and then prevent form submission from the login() function. Please check the below code.

In your form: <form name=loginForm onsubmit="login(event)" style="display: inline-block;">

Change Login function:

function login(e) {
  e.preventDefault();
  let uname = document.forms["loginForm"]["uname"].value;
  let pwd = document.forms["loginForm"]["psw"].value;
  window.location.href="main.html";
}

NOTE! Don't forget to pass event in login function

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

window.location.href - help redirecting

From Dev

JavaScript window.location not redirecting to a different page

From Dev

window.location | javascript redirecting

From Dev

Why isnt window.location.href= not forwarding to page using Safari?

From Dev

Javascript: Referencing href location in HTML pop up window

From Dev

Spring not redirecting to html page

From Dev

window.location.href and location.reload() does not load updated page from Server

From Dev

String is not a function on window location href

From Dev

window.location.href not working

From Dev

How to redirect to another html page without using window.location?

From Dev

app route - not redirecting to html page

From Dev

Javascript: How to catch error on page navigated to using window.location.href = url

From Dev

Make window.location.href load page from Server instead from browser cache

From Dev

Getting window.location.href to return page name and extension if URL ends with a / slash

From Dev

Make window.location.href load page from Server instead from browser cache

From Dev

Redirecting a page into the same tabbed window using JavaScript?

From Dev

on redirecting to a previous html page the page should not refresh

From Dev

html href not linking to correct location

From Dev

Enable and disable window.location.href

From Dev

onclick window.location.href with input value

From Dev

Javascript window.location.href onClick not working

From Dev

Passing parameter through "window.location.href"

From Dev

onchange event on window.location.href

From Dev

Window.location.href infinite loop

From Dev

Stubbing window.location.href with Sinon

From Dev

window.location.href not working in IE 11

From Dev

Using a variable for if window.location.href.indexOf()

From Dev

$window.location.href NOT WORKING in AngularJS

From Dev

window.location.href appending instead of replacing

Related Related

  1. 1

    window.location.href - help redirecting

  2. 2

    JavaScript window.location not redirecting to a different page

  3. 3

    window.location | javascript redirecting

  4. 4

    Why isnt window.location.href= not forwarding to page using Safari?

  5. 5

    Javascript: Referencing href location in HTML pop up window

  6. 6

    Spring not redirecting to html page

  7. 7

    window.location.href and location.reload() does not load updated page from Server

  8. 8

    String is not a function on window location href

  9. 9

    window.location.href not working

  10. 10

    How to redirect to another html page without using window.location?

  11. 11

    app route - not redirecting to html page

  12. 12

    Javascript: How to catch error on page navigated to using window.location.href = url

  13. 13

    Make window.location.href load page from Server instead from browser cache

  14. 14

    Getting window.location.href to return page name and extension if URL ends with a / slash

  15. 15

    Make window.location.href load page from Server instead from browser cache

  16. 16

    Redirecting a page into the same tabbed window using JavaScript?

  17. 17

    on redirecting to a previous html page the page should not refresh

  18. 18

    html href not linking to correct location

  19. 19

    Enable and disable window.location.href

  20. 20

    onclick window.location.href with input value

  21. 21

    Javascript window.location.href onClick not working

  22. 22

    Passing parameter through "window.location.href"

  23. 23

    onchange event on window.location.href

  24. 24

    Window.location.href infinite loop

  25. 25

    Stubbing window.location.href with Sinon

  26. 26

    window.location.href not working in IE 11

  27. 27

    Using a variable for if window.location.href.indexOf()

  28. 28

    $window.location.href NOT WORKING in AngularJS

  29. 29

    window.location.href appending instead of replacing

HotTag

Archive