When would you set location
to a URL string versus setting location.href
?
location = "http://www.stackoverflow.com";
vs
location.href = "http://www.stackoverflow.com";
When would you set location
to a URL string versus setting location.href
?
location = "http://www.stackoverflow.com";
vs
location.href = "http://www.stackoverflow.com";
You might set location
directly because it's slightly shorter. If you're trying to be terse, you can usually omit the window.
too.
URL assignments to both location.href
and location
are defined to work in JavaScript 1.0, back in Netscape 2, and have been implemented in every browser since. So take your pick and use whichever you find clearest.
Even if both work, I would use the latter.
window.location
: it does not require SAME ORIGIN
. –
Persas location = 'http://www.example.com'
seems super readable. Albeit, as a special case. That is backwards compatible and will remain compatible in the foreseeable future. –
Ostrogoth Like as has been said already. But, you will do better to use the .href
version.
location
works because the property was defined to have this special assignment behaviour back in JavaScript 1.0 and every browser since has implemented that. HTML5 now requires it. So whilst it may be prettier or more consistent to assign to .href
, there is no backward or forward compatibility advantage to doing so. –
Doura window.location = url
is prettier –
Recoil location = url
is cuter –
Certify location.href = url
is technically more correct and thus more safe (or fool-proof). In general, when you assign myObject.someProp = someThing
you do not expect that the value of someThing
will be assigned to myObject.someProp.someInnerProp
, right? Then why would you expect it from location.href
? It seems to be even dangerous to rely upon such behavior and assume that it will always work that quirky way. –
Cidevant Location
is a well-defined object and the specs around it are clear and implemented consistently across the board. So location = url
is equally correct. But shorter. And thus should, imho, be preferred in our perpetual quest to shave as much overhead off of our pages as possible. –
Pencel window.location.href = url
is stronger/smarter/bigger... =) –
Condorcet =
in JS. –
Claud A couple of years ago, location
did not work for me in IE and location.href
did (and both worked in other browsers). Since then I have always just used location.href
and never had trouble again. I can't remember which version of IE that was.
strict mode
chrome will throw an exception if you try to assign directly to location
too, so I always use location.href
–
Radioscope One difference to keep in mind, though.
Let's say you want to build some URL using the current URL. The following code will in fact redirect you, because it's not calling String.prototype.replace
but Location.prototype.replace
:
nextUrl = window.location.replace('/step1', '/step2');
The following codes work:
// cast to string
nextUrl = (window.location+'').replace('/step1', '/step2');
// href property
nextUrl = window.location.href.replace('/step1', '/step2');
Just to clarify, you can't do location.split('#')
, location
is an object, not a string. But you can do location.href.split('#');
because location.href
is a string.
With TypeScript, use window.location.href
as window.location
is technically an object containing:
Properties
hash
host
hostname
href <--- you need this
pathname (relative to the host)
port
protocol
search
Setting window.location
will produce a type error, while
window.location.href
is of type string.
Use global.location.href
instead, while working with React
.
© 2022 - 2024 — McMap. All rights reserved.
location.href
mail fail because of same-origin-policy: javascript.info/tutorial/… – Rowelreplace
andassign
: https://mcmap.net/q/93875/-what-39-s-the-difference-between-window-location-and-window-location-replace/632951 , https://mcmap.net/q/93876/-window-location-href-vs-window-location-replace-vs-window-location-assign-in-javascript/632951 , https://mcmap.net/q/93877/-location-href-property-vs-location-assign-method/632951 – Acaulescentlocation.href
policy: javascript.info/cross-window-communication#4q5rssu5ys – Snaggy