Definitely the second method is preferred because you don't have the overhead of another function invocation:
window.location.href = "webpage.htm";
Answer from Jacob Relkin on Stack OverflowDefinitely the second method is preferred because you don't have the overhead of another function invocation:
window.location.href = "webpage.htm";
Hopefully someone else is saved by reading this.
We encountered an issue with webkit based browsers doing:
window.open("webpage.htm", "_self");
The browser would lockup and die if we had too many DOM nodes. When we switched our code to following the accepted answer of:
location.href = "webpage.html";
all was good. It took us awhile to figure out what was causing the issue, since it wasn't obvious what made our page periodically fail to load.
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>
^^^^^^^^^^^^^
windows.open() opens the URL in a new window.
To replace the URL in the current window, use:
window.location.href = 'http://example.com';
Use _self instead of _blank.
window.open(url, "_self");
- _blank - URL is loaded into a new window. This is default
- _parent - URL is loaded into the parent frame
- _self - URL replaces the current page
- _top - URL replaces any framesets that may be loaded name - The name of the window
For further details. See This Link
I know that this question is uber old, but it perfectly described the problem I needed answered, and I was able to come up with a decent solution, so I thought I'd post it here. It's an interesting workaround.
Essentially you set a timeout to delay forwarding the current page to the new url, and then open the current url in a new tab. So:
function open_hidden_tab(new_url) {
var thisTimeout= setTimeout(function() {
window.location.href= new_url;
}, 500);
var newWindow= window.open(window.location.href);
if(!newWindow) {
clearTimeout(thisTimeout);
alert('Please allow pop-ups on this site!');
}
}
open_hidden_tab('https://google.com');
Obviously, you should show some kind of error message on your site instead of using the annoying alert function, but it works for the purposes of this example.
Hope this helps someone!