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

What is the difference between "window.location.href" and "window.location.hash"?

本文关键字:location window 什么 区别 hash href      更新时间:2023-09-26

我学习了"window.location.hash">,并尝试在我的jquery代码中而不是"window.location.href">,它们都给出了相同的结果。

代码在这里:

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

它们之间有什么区别?

对于像 http://[www.example.com]:80/search?q=devmo#test 这样的网址

  • hash - 返回 URL 中#符号后面的部分,包括#符号。您可以侦听 hashchange 事件,以便在支持的浏览器中收到哈希更改的通知。

    Returns: #test
    
  • href - 返回整个 URL。

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

阅读更多

例如

http://stackoverflow.com/#Page对其进行测试

href = http://stackoverflow.com/#Page
hash = #Page

href 是网址

哈希只是 url 后面的锚点

http://www.xxxxxxxx.com#anchor

http://www.xxxxxxxx.com#anchor is the href

"#anchor"是哈希

hash 属性返回 URL 的锚点部分,包括哈希符号 (#(。

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

这是window.location.hrefwindow.location.hash之间区别的简单示例

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

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