What's the preferred method to use to change the location of the current web page using JavaScript? I've seen both window.navigate and document.location used. Are there any differences in behavior? Are there differences in browser implementations?
window.location.href = 'URL';
is the standard implementation for changing the current window's location.
There really isn't a difference; there are about 5 different methods of doing it. However, the ones I see most often are document.location
and window.location
because they're supported by all major browsers. (I've personally never seen window.navigate
used in production code, so maybe it doesn't have very good support?)
I'd go with window.location = "http://...";
. I've been coding cross-browser JavaScript for a few years, and I've never experienced problems using this approach.
window.navigate
and window.location.href
seems a bit odd to me.
document.location is a (deprecated but still present) read-only string property, replaced by document.url.
location='url path' is the simplest expression to open a new page in the same window.