Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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

Is it the same across browsers?

like image 663
Michel Avatar asked Apr 16 '10 12:04

Michel


People also ask

What is document location href?

The location. href property sets or returns the entire URL of the current page.

What is difference between document location and window 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).

What is a document location?

The Document. location read-only property returns a Location object, which contains information about the URL of the document and provides methods for changing that URL and loading another URL. Though Document. location is a read-only Location object, you can also assign a string to it.


1 Answers

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.

like image 119
bobince Avatar answered Sep 22 '22 00:09

bobince