Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between window.location and window.location.href? [duplicate]

Tags:

javascript

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";
like image 850
Sanghyun Lee Avatar asked Jun 07 '11 09:06

Sanghyun Lee


1 Answers

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.

like image 78
lonesomeday Avatar answered Nov 12 '22 04:11

lonesomeday