【问题标题】:Check if url hash exists检查 url 哈希是否存在
【发布时间】:2018-04-27 10:16:58
【问题描述】:

我想检查 url 中是否有哈希:

这应该返回 true:

domain.com/balbla#hash

这应该返回 false:

domain.com/balbla

所以我使用URI.js 来获取哈希

var uriFragment = new URI(window.location.href).fragment();
if (uriFragment.length) {
    console.log('yep')
} else {
    console.log('nope')
}

但这永远不会返回 nope,即使 url 中没有哈希。

【问题讨论】:

    标签: javascript jquery uri uri.js


    【解决方案1】:

    为什么不直接使用location.hash

    if(location.hash.length > 0) {
        console.log('yep')
    }else {
       console.log('nop')
    }
    

    【讨论】:

      【解决方案2】:

      下面的代码将给出数组格式的哈希标签列表。

      var hash = window.location.hash;
          return result = hash.split('#').reduce(function (result, item) {
              var parts = item.split('=');
              result[parts[0]] = parts[1];
              return result;
          }, {});
      

      【讨论】:

        猜你喜欢
        • 2011-05-19
        • 1970-01-01
        • 2012-04-05
        • 1970-01-01
        • 2017-04-28
        • 2012-09-03
        • 2023-04-07
        • 1970-01-01
        • 2018-02-18
        相关资源
        最近更新 更多