What is the difference between document.location.href and document.location?

document.location is a synonym for window.location that has been deprecated for almost as long as JavaScript has existed. Don’t use it.

location is a structured object, with properties corresponding to the parts of the URL. location.href is the whole URL in a single string. Assigning a string to either is defined to cause the same kind of navigation, so take your pick.

I consider writing to location.href = something to be marginally better as it’s slightly more explicit about what it’s doing. You generally want to avoid just location = something as it looks misleadingly like a variable assignment. window.location = something is fine though.

Leave a Comment