Open page in the same window

Asked

Viewed 2,970 times

-2

I have a button where I would like it to direct to another page when clicked and the alternative I found was using om click, but it turns out it opens in another window and I need it to open in the same window.

How to open in the same window?

<button type="submit" onclick="window.open('menu.html')">Login</button>
  • 1

    Face your question is hard to interpret. You have a button of the kind Submit, this type of button submits something, it is usually placed in a form to submit it. If you put this function onclick in a Ubmit, the form is submitted, there will be no time to perform the function you want. You want to open a right menu, on the same page, on another page, what you really want to do?

  • tries to use "<a href="menu.html"><button type="Submit">Login</button></a>"

  • 1

    Why did you use <button> and not <a>?

1 answer

1


Use window.location.href='' in place of window.open('menu.html'), window.open will open a new window and that’s not what you want...
You can use too window.location.replace('menu.html'), but this will replace the current page in the browser history, making it not possible to return to the page after clicking the button.

Option 1 window.location.href='':

<button type="submit" onclick="window.location.href='menu.html'">Login</button>

Option 2 window.location.replace():

<button type="submit" onclick="window.location.replace('menu.html')">Login</button>

Option 3, a button made link.

.btnlogin {
    background:#000;
    border-radius:4px;
    padding:4px 6px;
    color:#fff;
    text-decoration:none;
}
<a href="menu.html" class="btnlogin">Login</a>

  • The answer on the question has already been answered, but I was left with a doubt. The riquired of imputs works with options 1 or 2?

  • required is individual input, the button does not affect it unless something changes it, such as a script document.querySelector("input").required = false or something like

Browser other questions tagged

You are not signed in. Login or sign up in order to post.