window.location和window.location.href之间有什么区别吗?

San*_*Lee 5 javascript

可能重复:
Javascript:设置window.location.href与window.location

当我在浏览器中测试这些代码时,它们似乎是相同的.有什么区别吗?

1

window.location = "http://stackoverflow.com";
Run Code Online (Sandbox Code Playgroud)

2

window.location.href = "http://stackoverflow.com";
Run Code Online (Sandbox Code Playgroud)

lon*_*day 20

是,有一点不同. window.location是一个Location对象. window.location.href是位置的字符串表示.该location对象的toString()值是相同的href属性,所以如果用作字符串它们是相同的.设置window.location与设置相同window.location.href.

window.location然而,有一些其他的属性,你可以使用,如location.hostname,location.pathnamelocation.hash.所以你可以location.hash自己设置来改变哈希值.