Should I use window.navigate or document.location in JavaScript?

Should I use window.navigate or document.location in JavaScript?

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?


Solution 1:

window.location.href = 'URL';

is the standard implementation for changing the current window’s location.

Solution 2:

document.location is a (deprecated but still present) read-only string property, replaced by document.url.

Solution 3:

window.location also affects to the frame,

the best form i found is:


And the worse is:


I did a massive browser test, and some rare IE with several plugins get undefined with the second form.

Solution 4:

window.location will affect to your browser target.
document.location will only affect to your browser and frame/iframe.

Solution 5:

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 = "";
window.location = "";

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.

Solution 6:

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

Related:  What is the difference between using constructor vs state = {} to declare state in react component?