javascript - 检查 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 中没有散列,它也永远不会返回。
解决方案
下面的代码将以数组格式给出哈希标签列表。
var hash = window.location.hash;
return result = hash.split('#').reduce(function (result, item) {
var parts = item.split('=');
result[parts[0]] = parts[1];
return result;
}, {});
推荐阅读
- r - 如何在 dplyr 中使用 mutate 动态修改变量?
- python - 为什么我的 lib 文件夹中有两个 python 版本?
- reactjs - 在 componentDidMount 之后反应不渲染组件
- c++ - 我应该使用哪种算法来计算给定的数学序列?
- r - 关于如何解析此文本文件以获取数据框的任何想法?
- android - 在 Firebase 中构建数据列表的最有效方法是什么?
- docker - 如何使用 traefik 2 执行 https 到 http 重定向
- reactjs - 为什么将静态依赖项传递给 useEffect 会导致每次渲染都调用它?
- sql - sql如何将多选字段转换为总计行
- google-cloud-platform - 您可以通过 GCP 命令行将 zip 文件上传到 Google 函数吗?