5

Possible Duplicate:
Javascript: Setting window.location.href versus window.location

When I tested these code in browser it seems like they are the same. Is there any difference?

1

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

2

window.location.href = "http://stackoverflow.com";
Community
  • 1
  • 1
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126

3 Answers3

22

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

window.location, however, has several other properties you can use, such as location.hostname, location.pathname and location.hash. So you could could set location.hash on its own to alter the hash value.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
2

windows.location adds an item to your history in that you can (or should be able to) click "Back" and go back to the current page. It is an Object.

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

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
2

window.location is an object with some properties, but window.location.href is only string. In window.location you have for example reload method.

piotrek
  • 1,333
  • 4
  • 17
  • 35