检查 url 哈希是否存在

Check if url hash exists

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

这应该return正确:

domain.com/balbla#hash

这应该return false:

domain.com/balbla

所以我用URI.js来抓哈希

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

但是那永远不会 returns nope 即使 url 中没有散列。

为什么不直接使用location.hash

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

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

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