javascripthtmlselfwindow.opencurrent-page

window.open('url', '_self') not working


So i wanted to open a new page replacing the current one, i found that the method should be putting the second parameter on _self but nothing happen... By the way, if i use the _blank parameter or i left it empty it opens in a new page. The rest of the function works good, but i can't find a way to close the current page and open the new one that i want. Here is the javascript and the html buttom that call the function.

    <button id="rgstr_btn" type="submit" class="btn btn-info" onClick="store()">Register</button>   
      <script>
                function store() {
                localStorage.setItem('nome', nome.value);
                localStorage.setItem('pw', pw.value);
                window.open('url', '_self');
            }
</script>

Solution

  • Button has a type attribute which defaults to submit: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type
    While this does not affect "everyday" buttons, if the button resides in a form, this way it will submit the form, and result in some page loading, which clashes with your own attempt. You can just add a type="button" attribute to the button to avoid that:

    <button id="rgstr_btn" type="button" class="btn btn-info" onClick="store()">Register</button>
                           ^^^^^^^^^^^^^