Is there a difference between window.location and window.location.href?

Possible duplicate:
Javascript: setting window.location.href and window.location

When I tested this code in a browser, it seems that they are the same. Is there any difference?

1

window.location = "http://stackoverflow.com"; 

2

 window.location.href = "http://stackoverflow.com"; 
+4
source share
3 answers

Yes, there is a difference. window.location is a Location object. window.location.href is a string representation of the location. The Location value of the toString() object is the same as the href property, so they are identical if they are used as strings. The window.location setting window.location same as the window.location.href setting.

window.location , however, has several other properties that you can use, such as location.hostname , location.pathname and location.hash . That way you could set location.hash yourself to change the hash value.

+19
source

windows.location adds an item to your story where you can (or should be able to) click "Back" and return to the current page. This is an object.

windows.location.href , on the other hand, is a string representation of window.location

+2
source

window.location is an object with some properties, but window.location.href is just a string. In window.location, for example, you can reload the method.

+2
source

All Articles