【问题标题】:What is the difference between "window.location.href" and "window.location.hash"?“window.location.href”和“window.location.hash”有什么区别?
【发布时间】:2012-05-28 03:20:53
【问题描述】:

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

代码在这里:

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

它们有什么区别?

【问题讨论】:

标签: javascript jquery hash href window.location


【解决方案1】:

对于像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

Read More

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      href 是网址

      hash 只是 url 后面的锚

      http://www.xxxxxxx.com#anchor

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

      “#anchor”是哈希

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 很确定它包含# 字符。
        【解决方案5】:

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

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

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-01-25
            • 2011-03-20
            • 2011-12-03
            • 2010-10-02
            • 2011-12-12
            • 2010-09-16
            • 2012-03-14
            相关资源
            最近更新 更多