首页 > 解决方案 > 检查 url 哈希是否存在

问题描述

我想检查 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 中没有散列,它也永远不会返回。

标签: javascriptjqueryuriuri.js

解决方案


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

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

推荐阅读