“window.location.href”和“window.location.hash”有什么区别?

时间:2012-05-21 15:46:35

标签: javascript jquery hash href window.location

我学习了“window.location.hash” new并尝试使用我的jquery代码而不是“window.location.href”,并且两者都给出了相同的结果。

代码在这里:

window.location.href = ($(e.currentTarget).attr("href"));
window.location.hash = ($(e.currentTarget).attr("href"));

他们之间有什么区别?

6 个答案:

答案 0 :(得分:58)

对于http://[www.example.com]:80/search?q=devmo#test

这样的网址

哈希返回#符号后面的网址部分,包括#符号。 您可以侦听hashchange事件,以获得有关支持浏览器中哈希更改的通知。

Returns: #test

href 会返回整个网址。

Returns: http://[www.example.com]:80/search?q=devmo#test

Read More

答案 1 :(得分:9)

例如http://stackoverflow.com/#Page

测试它
href = http://stackoverflow.com/#Page
hash = #Page

答案 2 :(得分:3)

href是网址

hash只是url之后的锚点

http://www.xxxxxxx.com#anchor

http://www.xxxxxxx.com#anchor是href

“#anchor”是哈希

答案 3 :(得分:2)

hashhref都是window.location对象的属性。 hash是来自#的网址的一部分(如果没有#,则为空字符串),而href是整个网址的字符串表示形式。

答案 4 :(得分:1)

hash属性返回URL的锚点部分,包括井号(#)。

答案 5 :(得分:0)

以下是window.location.hrefwindow.location.hash

之间差异的简单示例

对于网址http://www.manm.com/member/#!create

  • href:http://www.manam.com/member/#!create
  • 哈希:#!create