window.location和window.location.href之间有什么区别吗? [英] Is there any difference between window.location and window.location.href?

查看:78
本文介绍了window.location和window.location.href之间有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

2

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

推荐答案

是的,有区别. window.location Location 目的. window.location.href 是位置的字符串表示形式. location 对象的 toString()值与 href 属性相同,因此如果用作字符串,则它们是相同的.设置 window.location 与设置 window.location.href 相同.

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 还有其他一些可以使用的属性,例如 location.hostname location.pathname location.hash .因此,您可以自行设置 location.hash 来更改哈希值.

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.

这篇关于window.location和window.location.href之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆