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?
11 Answers
window.location.href = 'URL';
is the standard implementation for changing the current window's location.

- 49,340
- 32
- 108
- 136
-
29Do you have a reference to indicate that the window.location.href is the standard implementation? And does that standard apply equally well to all browsers? You certainly seem knowledgeable and 15+ votes (plus accepted answer) help make it more authoritative, though I think it would be better to see documentation from the browser development teams to back up the claim. – Goyuix Dec 09 '10 at 17:37
-
6@Goyuix, it's probably more accurate to say that `window.location.href` is the complete implementation, but `window.location` accomplishes the same thing. See http://docs.sun.com/source/816-6408-10/location.htm: "If you assign a string to the `location` property of an object, JavaScript creates a `location` object and assigns that string to its `href` property." – James Skidmore Dec 09 '10 at 18:36
-
3Looks like these guys decided it's personal preference: https://developer.mozilla.org/Talk:en/DOM/window.location. Or see the example near the bottom, they only use `window.location` but that doesn't necessarily indicate either way: https://developer.mozilla.org/en/window.location. – James Skidmore Dec 09 '10 at 18:41
-
1I'm currently using this for android development. I'm having trouble to get the page redirected from JS in Androids default browser. I tried `window.location.href = 'URL';` and also `window.location.assign('URL');` the method that is designed for reloading a new page. More information can be found http://www.w3schools.com/jsref/obj_location.asp – philipp Apr 12 '12 at 01:05
-
5`window.navigate` is a proprietary method, used by Internet Explorer (I am note sure whether other browsers mimics it for compatibility, Chrome does not). `document.location` or `window.location` are standard objects (see the various HTML/HTML5/DOM specifications). `document.location = someURL` (or `window.location = someURL`) is probably supported due to legacy code. The right way to do it is `document.location.href = someURL`, or perhaps `document.location.assign(someURL)`. – PhistucK Jun 08 '13 at 10:44
window.navigate not supported in some browser
In java script many ways are there for redirection, see the below code and explanation
window.location.href = "http://krishna.developerstips.com/";
window.location = "http://developerstips.com/";
window.location.replace("http://developerstips.com/");
window.location.assign("http://work.developerstips.com/");
window.location.href loads page from browser's cache and does not always send the request to the server. So, if you have an old version of the page available in the cache then it will redirect to there instead of loading a fresh page from the server.
window.location.assign() method for redirection if you want to allow the user to use the back button to go back to the original document.
window.location.replace() method if you want to want to redirect to a new page and don't allow the user to navigate to the original page using the back button.

- 4,366
- 2
- 39
- 46
window.location
will affect to your browser target.
document.location will only affect to your browser and frame/iframe.

- 27,717
- 28
- 128
- 190

- 61
- 1
- 1
window.location
also affects to the frame,
the best form i found is:
parent.window.location.href
And the worse is:
parent.document.URL
I did a massive browser test, and some rare IE with several plugins get undefined with the second form.
-
By that logic, wouldn't top.window.location.href be better still? – Orwellophile Jun 01 '12 at 12:16
document.location
is a (deprecated but still present) read-only string property, replaced by document.URL
.

- 1,291
- 17
- 29

- 102,654
- 32
- 106
- 127
-
5I couldn't find any information suggesting that `document.location` is deprecated. Please add source. – mikabytes Sep 14 '21 at 08:40
window.navigate
is NOT supported in some browsers, so that one should be avoided. Any of the other methods using the location property are the most reliable and consistent approach

- 27,717
- 28
- 128
- 190

- 41
- 1
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.

- 21,396
- 37
- 131
- 170
-
13window.location works, but it's technically incorrect because "location" is an object. – James Skidmore Jun 04 '09 at 02:21
-
17
-
2While pretty much everything is an object in JavaScript, assigning a string to an object does not generally set the value to one of its properties (as window.location or document.location do with their href property), but instead replaces that object with a string. In this case, a browser quirk was added in order to be compatible with existing (quirky) implementations and legacy (and not so legacy) content. – PhistucK Mar 16 '14 at 05:38
Late joining this conversation to shed light on a mildly interesting factoid for web-facing, analytics-aware websites. Passing the mic over to Michael Papworth:
https://github.com/michaelpapworth/jQuery.navigate
"When using website analytics, window.location is not sufficient due to the referer not being passed on the request. The plugin resolves this and allows for both aliased and parametrised URLs."
If one examines the code what it does is this:
var methods = {
'goTo': function (url) {
// instead of using window.location to navigate away
// we use an ephimeral link to click on and thus ensure
// the referer (current url) is always passed on to the request
$('<a></a>').attr("href", url)[0].click();
},
...
};
Neato!

- 3,786
- 2
- 36
- 56
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?)

- 127,549
- 26
- 102
- 115
-
document.location doesn't work in all browsers. window.location does. – Philippe Leybaert Jun 09 '09 at 19:38
-
2
support for document.location
is also good though its a deprecated method.
I've been using this method for a while with no problems.
you can refer here for more details:
https://developer.mozilla.org/en-US/docs/Web/API/document.location

- 27,717
- 28
- 128
- 190

- 36
- 9
You can move your page using
window.location.href =Url;

- 27,717
- 28
- 128
- 190

- 39
- 5
-
3This doesn't really add more info then the accepted, and multiply upvoted answer... – Stuart Siegler Jul 03 '15 at 13:10
-
3You could even go further and argue that you've simply plagiarised the accepted answer – Liam Jul 03 '15 at 14:00