What's the difference between clicking on:
<a href />
vs.
calling window.location.href = ...
?
Location assign() Method location. replace("http://someUrl.com");
All attributes are optional, although one of NAME and HREF is necessary for the anchor to be useful.
Wherever possible, you should use <a href="foo.html">
over window.location.href
, for a number of very good reasons.
window.location
breaks all of theseSetting window.location.href = 'thepage.html'
is the same as calling:
window.open('thepage.html', '_self');
I.e. the target is limited to the same window, as that is where the location property is. This has the same effect as clicking a link without a target attribute:
<a href="thepage.html">...</a>
You can use the open method instead to specify a different target, like a new window:
window.open('thepage.html', '_blank');
This has the same effect as clicking a link with that target:
<a href="thepage.html" target="_blank">...</a>
You can also use the open method to open a new window. The return value is a reference to the window, so you can use that to set the location of that window instead of the current window:
var w = window.open('about:blank', '_blank'); w.location.href = 'thepage.html';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With