Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is the difference between window location and document location?

window. location is read/write on all compliant browsers. document. location is read-only in Internet Explorer (at least), but read/write in Gecko-based browsers (Firefox, SeaMonkey).

Is it good to use window location href?

See the question IE incompatability with window. location. href. But yes, it's better to use href as a property, which will work in any browser, including IE.

What is document window and location JavaScript?

The location property of a window (i.e. window. location ) is a reference to a Location object; it represents the current URL of the document being displayed in that window. Since window object is at the top of the scope chain, so properties of the window. location object can be accessed without window.


window.location.href = 'URL';

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


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.


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.


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


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


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